Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Db/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Context extends EntitySuper implements JsonSerializable {

public function __construct() {
$this->addType('id', 'integer');
$this->addType('owner_type', 'integer');
$this->addType('ownerType', 'integer');
}

public function jsonSerialize(): array {
Expand Down
6 changes: 3 additions & 3 deletions lib/Db/ContextNodeRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class ContextNodeRelation extends Entity implements \JsonSerializable {

public function __construct() {
$this->addType('id', 'integer');
$this->addType('context_id', 'integer');
$this->addType('node_id', 'integer');
$this->addType('node_type', 'integer');
$this->addType('contextId', 'integer');
$this->addType('nodeId', 'integer');
$this->addType('nodeType', 'integer');
$this->addType('permissions', 'integer');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Db/PageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class PageContent extends Entity implements \JsonSerializable {

public function __construct() {
$this->addType('id', 'integer');
$this->addType('page_id', 'integer');
$this->addType('node_rel_id', 'integer');
$this->addType('pageId', 'integer');
$this->addType('nodeRelId', 'integer');
$this->addType('order', 'integer');
}

Expand Down
107 changes: 107 additions & 0 deletions tests/unit/Db/EntityFieldTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Tests\Unit\Db;

use OCA\Tables\Db\Context;
use OCA\Tables\Db\ContextMapper;
use OCA\Tables\Db\ContextNodeRelation;
use OCA\Tables\Db\ContextNodeRelationMapper;
use OCA\Tables\Db\MenuItemMapper;
use OCA\Tables\Db\PageContent;
use OCA\Tables\Db\PageContentMapper;
use OCA\Tables\Helper\GroupHelper;
use OCA\Tables\Helper\UserHelper;
use OCA\Tables\Tests\Unit\Database\DatabaseTestCase;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;

/**
* Runs against the database: the integer columns of applications, their node relations
* and page contents are typed by property name, so they are written and read as integers.
*/
class EntityFieldTypesTest extends DatabaseTestCase {
/**
* @return array<string, array{class-string<Entity>, list<string>}>
*/
public static function integerProperties(): array {
return [
'application' => [Context::class, ['ownerType']],
'node relation' => [ContextNodeRelation::class, ['contextId', 'nodeId', 'nodeType', 'permissions']],
'page content' => [PageContent::class, ['pageId', 'nodeRelId', 'order']],
];
}

/**
* @dataProvider integerProperties
* @param class-string<Entity> $class
* @param list<string> $properties
*/
public function testEveryFieldTypeNamesAProperty(string $class, array $properties): void {
$types = (new $class())->getFieldTypes();

$this->assertSame([], array_values(array_filter(array_keys($types), static fn (string $field): bool => !property_exists($class, $field))));
foreach ($properties as $property) {
$this->assertSame('integer', $types[$property] ?? null, $property);
}
}

/**
* @dataProvider integerProperties
* @param class-string<Entity> $class
* @param list<string> $properties
*/
public function testTheMapperBindsTheIntegerColumnsAsIntegers(string $class, array $properties): void {
$mapper = $this->mapperFor($class);
$parameterType = new \ReflectionMethod(QBMapper::class, 'getParameterTypeForProperty');

foreach ($properties as $property) {
$this->assertSame(IQueryBuilder::PARAM_INT, $parameterType->invoke($mapper, new $class(), $property), $property);
}
}

public function testANodeRelationAndAPageContentAreReadBackAsIntegers(): void {
$relationMapper = new ContextNodeRelationMapper($this->connectionAdapter);
$contentMapper = new PageContentMapper($this->connectionAdapter);
$relation = new ContextNodeRelation();
$relation->setContextId(990701);
$relation->setNodeId(990702);
$relation->setNodeType(1);
$relation->setPermissions(3);
$relation = $relationMapper->insert($relation);
$content = new PageContent();
$content->setPageId(990703);
$content->setNodeRelId($relation->getId());
$content->setOrder(20);
$content = $contentMapper->insert($content);

try {
$readRelation = $relationMapper->findById($relation->getId());
$readContent = $contentMapper->findById($content->getId());

$this->assertSame([990701, 990702, 1, 3], [$readRelation->getContextId(), $readRelation->getNodeId(), $readRelation->getNodeType(), $readRelation->getPermissions()]);
$this->assertSame([990703, $relation->getId(), 20], [$readContent->getPageId(), $readContent->getNodeRelId(), $readContent->getOrder()]);
} finally {
$contentMapper->delete($content);
$relationMapper->delete($relation);
}
}

/**
* @param class-string<Entity> $class
*/
private function mapperFor(string $class): QBMapper {
return match ($class) {
Context::class => new ContextMapper($this->connectionAdapter, $this->createMock(UserHelper::class), $this->createMock(GroupHelper::class), new MenuItemMapper($this->connectionAdapter)),
ContextNodeRelation::class => new ContextNodeRelationMapper($this->connectionAdapter),
PageContent::class => new PageContentMapper($this->connectionAdapter),
};
}
}
Loading