fix(tests): Fix PHPUnit deprecation warnings in tests

Turn data providers into static methods, mostly.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/54905/head
Côme Chilliet 4 weeks ago
parent 1bbf12c465
commit ac61839d1e
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
  1. 48
      apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php
  2. 99
      apps/settings/tests/SetupChecks/WellKnownUrlsTest.php
  3. 29
      apps/theming/tests/Settings/PersonalTest.php
  4. 4
      autotest.sh
  5. 197
      tests/lib/Collaboration/Collaborators/GroupPluginTest.php
  6. 160
      tests/lib/Collaboration/Collaborators/UserPluginTest.php
  7. 203
      tests/lib/DB/QueryBuilder/QueryBuilderTest.php

@ -4,6 +4,7 @@
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
*/ */
namespace OCA\Files_Sharing\Tests\Middleware; namespace OCA\Files_Sharing\Tests\Middleware;
use OCA\Files_Sharing\Controller\ShareAPIController; use OCA\Files_Sharing\Controller\ShareAPIController;
@ -13,18 +14,16 @@ use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\IL10N; use OCP\IL10N;
use OCP\Share\IManager; use OCP\Share\IManager;
use PHPUnit\Framework\MockObject\MockObject;
/** /**
* @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
*/ */
class OCSShareAPIMiddlewareTest extends \Test\TestCase { class OCSShareAPIMiddlewareTest extends \Test\TestCase {
private IManager&MockObject $shareManager;
private IL10N&MockObject $l;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ private OCSShareAPIMiddleware $middleware;
private $shareManager;
/** @var IL10N */
private $l;
/** @var OCSShareAPIMiddleware */
private $middleware;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -37,49 +36,44 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase {
$this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l); $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
} }
public function dataBeforeController() { public static function dataBeforeController() {
return [ return [
[ [
$this->createMock(Controller::class), Controller::class,
false, false,
false false
], ],
[ [
$this->createMock(Controller::class), Controller::class,
true, true,
false false
], ],
[ [
$this->createMock(OCSController::class), OCSController::class,
false, false,
false false
], ],
[ [
$this->createMock(OCSController::class), OCSController::class,
true, true,
false false
], ],
[ [
$this->createMock(ShareAPIController::class), ShareAPIController::class,
false, false,
true true
], ],
[ [
$this->createMock(ShareAPIController::class), ShareAPIController::class,
true, true,
false false
], ],
]; ];
} }
/**
*
* @param Controller $controller
* @param bool $enabled
* @param bool $exception
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataBeforeController')] #[\PHPUnit\Framework\Attributes\DataProvider('dataBeforeController')]
public function testBeforeController(Controller $controller, $enabled, $exception): void { public function testBeforeController(string $controllerClass, bool $enabled, bool $exception): void {
$controller = $this->createMock($controllerClass);
$this->shareManager->method('shareApiEnabled')->willReturn($enabled); $this->shareManager->method('shareApiEnabled')->willReturn($enabled);
try { try {
@ -93,24 +87,20 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase {
public function dataAfterController() { public function dataAfterController() {
return [ return [
[ [
$this->createMock(Controller::class), Controller::class,
], ],
[ [
$this->createMock(OCSController::class), OCSController::class,
], ],
[ [
$this->createMock(ShareAPIController::class), ShareAPIController::class,
], ],
]; ];
} }
/**
*
* @param Controller $controller
* @param bool $called
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAfterController')] #[\PHPUnit\Framework\Attributes\DataProvider('dataAfterController')]
public function testAfterController(Controller $controller): void { public function testAfterController(string $controllerClass): void {
$controller = $this->createMock($controllerClass);
if ($controller instanceof ShareAPIController) { if ($controller instanceof ShareAPIController) {
$controller->expects($this->once())->method('cleanup'); $controller->expects($this->once())->method('cleanup');
} }

@ -99,12 +99,34 @@ class WellKnownUrlsTest extends TestCase {
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestResponses')] #[\PHPUnit\Framework\Attributes\DataProvider('dataTestResponses')]
public function testResponses($responses, string $expectedSeverity): void { public function testResponses($responses, string $expectedSeverity): void {
$createResponse = function (int $statuscode, array $header = []): IResponse&MockObject {
$response = $this->createMock(IResponse::class);
$response->expects($this->any())
->method('getStatusCode')
->willReturn($statuscode);
$response->expects($this->any())
->method('getHeader')
->willReturnCallback(fn ($name) => $header[$name] ?? '');
return $response;
};
$this->config $this->config
->expects($this->once()) ->expects($this->once())
->method('getSystemValueBool') ->method('getSystemValueBool')
->with('check_for_working_wellknown_setup') ->with('check_for_working_wellknown_setup')
->willReturn(true); ->willReturn(true);
/* Use generate to mock a Generator, and $createResponse to create the response objects */
$responses = array_map(
fn (array $items) => $this->generate(
array_map(
fn (array $item) => $createResponse(...$item),
$items,
)
),
$responses,
);
$this->setupcheck $this->setupcheck
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
->method('runRequest') ->method('runRequest')
@ -114,90 +136,79 @@ class WellKnownUrlsTest extends TestCase {
$this->assertEquals($expectedSeverity, $result->getSeverity()); $this->assertEquals($expectedSeverity, $result->getSeverity());
} }
public function dataTestResponses(): array { public static function dataTestResponses(): array {
$createResponse = function (int $statuscode, array $header = []): IResponse&MockObject {
$response = $this->createMock(IResponse::class);
$response->expects($this->any())
->method('getStatusCode')
->willReturn($statuscode);
$response->expects($this->any())
->method('getHeader')
->willReturnCallback(fn ($name) => $header[$name] ?? '');
return $response;
};
$wellKnownHeader = ['X-NEXTCLOUD-WELL-KNOWN' => 'yes']; $wellKnownHeader = ['X-NEXTCLOUD-WELL-KNOWN' => 'yes'];
return [ return [
'expected codes' => [ 'expected codes' => [
[ [
$this->generate([$createResponse(200, $wellKnownHeader)]), [[200, $wellKnownHeader]],
$this->generate([$createResponse(200, $wellKnownHeader)]), [[200, $wellKnownHeader]],
$this->generate([$createResponse(207)]), [[207]],
$this->generate([$createResponse(207)]), [[207]],
], ],
SetupResult::SUCCESS, SetupResult::SUCCESS,
], ],
'late response with expected codes' => [ 'late response with expected codes' => [
[ [
$this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]), [[404], [200, $wellKnownHeader]],
$this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]), [[404], [200, $wellKnownHeader]],
$this->generate([$createResponse(404), $createResponse(207)]), [[404], [207]],
$this->generate([$createResponse(404), $createResponse(207)]), [[404], [207]],
], ],
SetupResult::SUCCESS, SetupResult::SUCCESS,
], ],
'working but disabled webfinger' => [ 'working but disabled webfinger' => [
[ [
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(207)]), [[207]],
$this->generate([$createResponse(207)]), [[207]],
], ],
SetupResult::SUCCESS, SetupResult::SUCCESS,
], ],
'unauthorized webdav but with correct configured redirect' => [ 'unauthorized webdav but with correct configured redirect' => [
[ [
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/'])]), [[401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/']]],
$this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/'])]), [[401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/']]],
], ],
SetupResult::SUCCESS, SetupResult::SUCCESS,
], ],
'not configured path' => [ 'not configured path' => [
[ [
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(404)]), [[404]],
], ],
SetupResult::WARNING, SetupResult::WARNING,
], ],
'Invalid webfinger' => [ 'Invalid webfinger' => [
[ [
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(207)]), [[207]],
$this->generate([$createResponse(207)]), [[207]],
], ],
SetupResult::WARNING, SetupResult::WARNING,
], ],
'Invalid nodeinfo' => [ 'Invalid nodeinfo' => [
[ [
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(207)]), [[207]],
$this->generate([$createResponse(207)]), [[207]],
], ],
SetupResult::WARNING, SetupResult::WARNING,
], ],
'Invalid caldav' => [ 'Invalid caldav' => [
[ [
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(404, $wellKnownHeader)]), [[404, $wellKnownHeader]],
$this->generate([$createResponse(404)]), [[404]],
$this->generate([$createResponse(207)]), [[207]],
], ],
SetupResult::WARNING, SetupResult::WARNING,
], ],

@ -29,6 +29,7 @@ use OCP\IL10N;
use OCP\INavigationManager; use OCP\INavigationManager;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserSession; use OCP\IUserSession;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
@ -69,28 +70,30 @@ class PersonalTest extends TestCase {
); );
} }
public function dataTestGetForm(): array { public static function dataTestGetForm(): array {
return [ return [
['', [ ['', [
$this->formatThemeForm('default'), 'default',
$this->formatThemeForm('light'), 'light',
$this->formatThemeForm('dark'), 'dark',
$this->formatThemeForm('light-highcontrast'), 'light-highcontrast',
$this->formatThemeForm('dark-highcontrast'), 'dark-highcontrast',
$this->formatThemeForm('opendyslexic'), 'opendyslexic',
]], ]],
['dark', [ ['dark', [
$this->formatThemeForm('dark'), 'dark',
$this->formatThemeForm('opendyslexic'), 'opendyslexic',
]], ]],
]; ];
} }
/** #[DataProvider('dataTestGetForm')]
* @param string[] $enabledThemes
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetForm')]
public function testGetForm(string $enforcedTheme, array $themesState): void { public function testGetForm(string $enforcedTheme, array $themesState): void {
$themesState = array_map(
$this->formatThemeForm(...),
$themesState
);
$this->config->expects($this->once()) $this->config->expects($this->once())
->method('getSystemValueString') ->method('getSystemValueString')
->with('enforce_theme', '') ->with('enforce_theme', '')

@ -396,8 +396,8 @@ function execute_tests {
echo "No coverage" echo "No coverage"
fi fi
echo "$PHPUNIT" --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3" echo "$PHPUNIT" --fail-on-warning --fail-on-risky --display-warnings --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
"$PHPUNIT" --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3" "$PHPUNIT" --fail-on-warning --fail-on-risky --display-warnings --display-deprecations --display-phpunit-deprecations --colors=always --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
RESULT=$? RESULT=$?
if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
@ -16,32 +18,23 @@ use OCP\IGroupManager;
use OCP\IUser; use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share\IShare; use OCP\Share\IShare;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class GroupPluginTest extends TestCase { class GroupPluginTest extends TestCase {
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private IConfig&MockObject $config;
protected $config; private IGroupManager&MockObject $groupManager;
private IUserSession&MockObject $session;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ private IUser&MockObject $user;
protected $groupManager;
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ protected ISearchResult $searchResult;
protected $session;
/** @var ISearchResult */
protected $searchResult;
/** @var GroupPlugin */ /** @var GroupPlugin */
protected $plugin; protected $plugin;
/** @var int */ protected int $limit = 2;
protected $limit = 2; protected int $offset = 0;
/** @var int */
protected $offset = 0;
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -57,7 +50,7 @@ class GroupPluginTest extends TestCase {
$this->user = $this->getUserMock('admin', 'Administrator'); $this->user = $this->getUserMock('admin', 'Administrator');
} }
public function instantiatePlugin() { public function instantiatePlugin(): void {
// cannot be done within setUp, because dependent mocks needs to be set // cannot be done within setUp, because dependent mocks needs to be set
// up with configuration etc. first // up with configuration etc. first
$this->plugin = new GroupPlugin( $this->plugin = new GroupPlugin(
@ -67,7 +60,7 @@ class GroupPluginTest extends TestCase {
); );
} }
public function getUserMock($uid, $displayName) { public function getUserMock(string $uid, string $displayName): IUser&MockObject {
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user->expects($this->any()) $user->expects($this->any())
@ -81,13 +74,7 @@ class GroupPluginTest extends TestCase {
return $user; return $user;
} }
/** protected function getGroupMock(string $gid, ?string $displayName = null, bool $hide = false): IGroup&MockObject {
* @param string $gid
* @param null $displayName
* @param bool $hide
* @return IGroup|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getGroupMock($gid, $displayName = null, $hide = false) {
$group = $this->createMock(IGroup::class); $group = $this->createMock(IGroup::class);
$group->expects($this->any()) $group->expects($this->any())
@ -109,7 +96,7 @@ class GroupPluginTest extends TestCase {
return $group; return $group;
} }
public function dataGetGroups(): array { public static function dataGetGroups(): array {
return [ return [
['test', false, true, false, [], [], [], [], true, false], ['test', false, true, false, [], [], [], [], true, false],
['test', false, false, false, [], [], [], [], true, false], ['test', false, false, false, [], [], [], [], true, false],
@ -118,7 +105,7 @@ class GroupPluginTest extends TestCase {
// group without display name // group without display name
[ [
'test', false, true, false, 'test', false, true, false,
[$this->getGroupMock('test1')], [['test1']],
[], [],
[], [],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -128,7 +115,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by id // group with display name, search by id
[ [
'test', false, true, false, 'test', false, true, false,
[$this->getGroupMock('test1', 'Test One')], [['test1', 'Test One']],
[], [],
[], [],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -138,7 +125,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by display name // group with display name, search by display name
[ [
'one', false, true, false, 'one', false, true, false,
[$this->getGroupMock('test1', 'Test One')], [['test1', 'Test One']],
[], [],
[], [],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
@ -148,7 +135,7 @@ class GroupPluginTest extends TestCase {
// group with display name, search by display name, exact expected // group with display name, search by display name, exact expected
[ [
'Test One', false, true, false, 'Test One', false, true, false,
[$this->getGroupMock('test1', 'Test One')], [['test1', 'Test One']],
[], [],
[['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
[], [],
@ -157,7 +144,7 @@ class GroupPluginTest extends TestCase {
], ],
[ [
'test', false, false, false, 'test', false, false, false,
[$this->getGroupMock('test1')], [['test1']],
[], [],
[], [],
[], [],
@ -167,8 +154,8 @@ class GroupPluginTest extends TestCase {
[ [
'test', false, true, false, 'test', false, true, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
@ -179,8 +166,8 @@ class GroupPluginTest extends TestCase {
[ [
'test', false, false, false, 'test', false, false, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
@ -191,8 +178,8 @@ class GroupPluginTest extends TestCase {
[ [
'test', false, true, false, 'test', false, true, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[], [],
@ -201,25 +188,25 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']], ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
], ],
false, false,
null, false,
], ],
[ [
'test', false, false, false, 'test', false, false, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[], [],
[], [],
true, true,
null, false,
], ],
[ [
'test', false, true, false, 'test', false, true, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[ [
@ -230,13 +217,13 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']], ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
], ],
false, false,
$this->getGroupMock('test'), ['test'],
], ],
[ [
'test', false, false, false, 'test', false, false, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[ [
@ -244,17 +231,17 @@ class GroupPluginTest extends TestCase {
], ],
[], [],
true, true,
$this->getGroupMock('test'), ['test'],
], ],
['test', true, true, false, [], [], [], [], true, false], ['test', true, true, false, [], [], [], [], true, false],
['test', true, false, false, [], [], [], [], true, false], ['test', true, false, false, [], [], [], [], true, false],
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test1'), ['test1'],
$this->getGroupMock('test2'), ['test2'],
], ],
[$this->getGroupMock('test1')], [['test1']],
[], [],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false, false,
@ -263,10 +250,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test1'), ['test1'],
$this->getGroupMock('test2'), ['test2'],
], ],
[$this->getGroupMock('test1')], [['test1']],
[], [],
[], [],
true, true,
@ -275,10 +262,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test')], [['test']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[], [],
false, false,
@ -287,10 +274,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test')], [['test']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[], [],
true, true,
@ -299,10 +286,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test1')], [['test1']],
[], [],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false, false,
@ -311,10 +298,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test1')], [['test1']],
[], [],
[], [],
true, true,
@ -323,10 +310,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]], [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
false, false,
@ -335,10 +322,10 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test'), ['test'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]], [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
[], [],
true, true,
@ -347,37 +334,37 @@ class GroupPluginTest extends TestCase {
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[], [],
[ [
['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']], ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']], ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
], ],
false, false,
null, false,
], ],
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[], [],
[], [],
true, true,
null, false,
], ],
[ [
'test', true, true, false, 'test', true, true, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[ [
['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']], ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
], ],
@ -386,27 +373,27 @@ class GroupPluginTest extends TestCase {
['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']], ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
], ],
false, false,
$this->getGroupMock('test'), ['test'],
], ],
[ [
'test', true, false, false, 'test', true, false, false,
[ [
$this->getGroupMock('test0'), ['test0'],
$this->getGroupMock('test1'), ['test1'],
], ],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], [['test'], ['test0'], ['test1']],
[ [
['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']], ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
], ],
[], [],
true, true,
$this->getGroupMock('test'), ['test'],
], ],
[ [
'test', false, false, false, 'test', false, false, false,
[ [
$this->getGroupMock('test', null, true), ['test', null, true],
$this->getGroupMock('test1'), ['test1'],
], ],
[], [],
[], [],
@ -417,20 +404,7 @@ class GroupPluginTest extends TestCase {
]; ];
} }
/** #[DataProvider('dataGetGroups')]
*
* @param string $searchTerm
* @param bool $shareWithGroupOnly
* @param bool $shareeEnumeration
* @param bool $groupSharingDisabled
* @param array $groupResponse
* @param array $userGroupsResponse
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param bool|IGroup $singleGroup
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetGroups')]
public function testSearch( public function testSearch(
string $searchTerm, string $searchTerm,
bool $shareWithGroupOnly, bool $shareWithGroupOnly,
@ -441,8 +415,19 @@ class GroupPluginTest extends TestCase {
array $exactExpected, array $exactExpected,
array $expected, array $expected,
bool $reachedEnd, bool $reachedEnd,
$singleGroup, array|false $singleGroup,
): void { ): void {
$groupResponse = array_map(
fn ($args) => $this->getGroupMock(...$args),
$groupResponse
);
if (is_array($singleGroup)) {
$singleGroup = $this->getGroupMock(...$singleGroup);
}
$userGroupsResponse = array_map(
fn ($args) => $this->getGroupMock(...$args),
$userGroupsResponse
);
$this->config->expects($this->any()) $this->config->expects($this->any())
->method('getAppValue') ->method('getAppValue')
->willReturnCallback( ->willReturnCallback(

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
@ -12,34 +14,24 @@ use OC\Collaboration\Collaborators\UserPlugin;
use OC\KnownUser\KnownUserService; use OC\KnownUser\KnownUserService;
use OCP\Collaboration\Collaborators\ISearchResult; use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share\IShare; use OCP\Share\IShare;
use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class UserPluginTest extends TestCase { class UserPluginTest extends TestCase {
/** @var IConfig|MockObject */ private IConfig&MockObject $config;
protected $config; private IUserManager&MockObject $userManager;
private IGroupManager&MockObject $groupManager;
/** @var IUserManager|MockObject */ private IUserSession&MockObject $session;
protected $userManager; private KnownUserService&MockObject $knownUserService;
private IUserStatusManager&MockObject $userStatusManager;
/** @var IGroupManager|MockObject */ private IUser&MockObject $user;
protected $groupManager;
/** @var IUserSession|MockObject */
protected $session;
/** @var KnownUserService|MockObject */
protected $knownUserService;
/** @var IUserStatusManager|MockObject */
protected $userStatusManager;
/** @var UserPlugin */ /** @var UserPlugin */
protected $plugin; protected $plugin;
@ -51,9 +43,6 @@ class UserPluginTest extends TestCase {
protected int $offset = 0; protected int $offset = 0;
/** @var IUser|MockObject */
protected $user;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -74,7 +63,7 @@ class UserPluginTest extends TestCase {
$this->user = $this->getUserMock('admin', 'Administrator'); $this->user = $this->getUserMock('admin', 'Administrator');
} }
public function instantiatePlugin() { public function instantiatePlugin(): void {
// cannot be done within setUp, because dependent mocks needs to be set // cannot be done within setUp, because dependent mocks needs to be set
// up with configuration etc. first // up with configuration etc. first
$this->plugin = new UserPlugin( $this->plugin = new UserPlugin(
@ -87,7 +76,7 @@ class UserPluginTest extends TestCase {
); );
} }
public function mockConfig($mockedSettings) { public function mockConfig($mockedSettings): void {
$this->config->expects($this->any()) $this->config->expects($this->any())
->method('getAppValue') ->method('getAppValue')
->willReturnCallback( ->willReturnCallback(
@ -97,7 +86,7 @@ class UserPluginTest extends TestCase {
); );
} }
public function getUserMock($uid, $displayName, $enabled = true, $groups = []) { public function getUserMock(string $uid, string $displayName, bool $enabled = true, array $groups = []): IUser&MockObject {
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user->expects($this->any()) $user->expects($this->any())
@ -115,17 +104,7 @@ class UserPluginTest extends TestCase {
return $user; return $user;
} }
public function getGroupMock($gid) { public static function dataGetUsers(): array {
$group = $this->createMock(IGroup::class);
$group->expects($this->any())
->method('getGID')
->willReturn($gid);
return $group;
}
public function dataGetUsers(): array {
return [ return [
['test', false, true, [], [], [], [], true, false], ['test', false, true, [], [], [], [], true, false],
['test', false, false, [], [], [], [], true, false], ['test', false, false, [], [], [], [], true, false],
@ -135,33 +114,33 @@ class UserPluginTest extends TestCase {
'test', false, true, [], [], 'test', false, true, [], [],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'), ], [], true, ['test', 'Test'],
], ],
[ [
'test', false, false, [], [], 'test', false, false, [], [],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'), ], [], true, ['test', 'Test'],
], ],
[ [
'test', true, true, [], [], 'test', true, true, [], [],
[], [], true, $this->getUserMock('test', 'Test'), [], [], true, ['test', 'Test'],
], ],
[ [
'test', true, false, [], [], 'test', true, false, [], [],
[], [], true, $this->getUserMock('test', 'Test'), [], [], true, ['test', 'Test'],
], ],
[ [
'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]], 'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'), ], [], true, ['test', 'Test'],
], ],
[ [
'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]], 'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
], [], true, $this->getUserMock('test', 'Test'), ], [], true, ['test', 'Test'],
], ],
[ [
'test', 'test',
@ -169,7 +148,7 @@ class UserPluginTest extends TestCase {
true, true,
[], [],
[ [
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
], ],
[], [],
[ [
@ -184,7 +163,7 @@ class UserPluginTest extends TestCase {
false, false,
[], [],
[ [
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
], ],
[], [],
[], [],
@ -197,8 +176,8 @@ class UserPluginTest extends TestCase {
true, true,
[], [],
[ [
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
$this->getUserMock('test2', 'Test Two'), ['test2', 'Test Two'],
], ],
[], [],
[ [
@ -214,8 +193,8 @@ class UserPluginTest extends TestCase {
false, false,
[], [],
[ [
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
$this->getUserMock('test2', 'Test Two'), ['test2', 'Test Two'],
], ],
[], [],
[], [],
@ -228,9 +207,9 @@ class UserPluginTest extends TestCase {
true, true,
[], [],
[ [
$this->getUserMock('test0', 'Test'), ['test0', 'Test'],
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
$this->getUserMock('test2', 'Test Two'), ['test2', 'Test Two'],
], ],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -248,9 +227,9 @@ class UserPluginTest extends TestCase {
true, true,
[], [],
[ [
$this->getUserMock('test0', 'Test'), ['test0', 'Test'],
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
$this->getUserMock('test2', 'Test Two'), ['test2', 'Test Two'],
], ],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -270,9 +249,9 @@ class UserPluginTest extends TestCase {
false, false,
[], [],
[ [
$this->getUserMock('test0', 'Test'), ['test0', 'Test'],
$this->getUserMock('test1', 'Test One'), ['test1', 'Test One'],
$this->getUserMock('test2', 'Test Two'), ['test2', 'Test Two'],
], ],
[ [
['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'], ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
@ -296,7 +275,7 @@ class UserPluginTest extends TestCase {
], ],
true, true,
false, false,
[['test1', $this->getUserMock('test1', 'Test One')]], [['test1', ['test1', 'Test One']]],
], ],
[ [
'test', 'test',
@ -311,7 +290,7 @@ class UserPluginTest extends TestCase {
[], [],
true, true,
false, false,
[['test1', $this->getUserMock('test1', 'Test One')]], [['test1', ['test1', 'Test One']]],
], ],
[ [
'test', 'test',
@ -336,8 +315,8 @@ class UserPluginTest extends TestCase {
true, true,
false, false,
[ [
['test1', $this->getUserMock('test1', 'Test One')], ['test1', ['test1', 'Test One']],
['test2', $this->getUserMock('test2', 'Test Two')], ['test2', ['test2', 'Test Two']],
], ],
], ],
[ [
@ -360,8 +339,8 @@ class UserPluginTest extends TestCase {
true, true,
false, false,
[ [
['test1', $this->getUserMock('test1', 'Test One')], ['test1', ['test1', 'Test One']],
['test2', $this->getUserMock('test2', 'Test Two')], ['test2', ['test2', 'Test Two']],
], ],
], ],
[ [
@ -386,8 +365,8 @@ class UserPluginTest extends TestCase {
false, false,
false, false,
[ [
['test', $this->getUserMock('test', 'Test One')], ['test', ['test', 'Test One']],
['test2', $this->getUserMock('test2', 'Test Two')], ['test2', ['test2', 'Test Two']],
], ],
], ],
[ [
@ -410,40 +389,34 @@ class UserPluginTest extends TestCase {
true, true,
false, false,
[ [
['test', $this->getUserMock('test', 'Test One')], ['test', ['test', 'Test One']],
['test2', $this->getUserMock('test2', 'Test Two')], ['test2', ['test2', 'Test Two']],
], ],
], ],
]; ];
} }
/** #[DataProvider('dataGetUsers')]
*
* @param string $searchTerm
* @param bool $shareWithGroupOnly
* @param bool $shareeEnumeration
* @param array $groupResponse
* @param array $userResponse
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param bool|IUser $singleUser
* @param array $users
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetUsers')]
public function testSearch( public function testSearch(
$searchTerm, string $searchTerm,
$shareWithGroupOnly, bool $shareWithGroupOnly,
$shareeEnumeration, bool $shareeEnumeration,
array $groupResponse, array $groupResponse,
array $userResponse, array $userResponse,
array $exactExpected, array $exactExpected,
array $expected, array $expected,
$reachedEnd, bool $reachedEnd,
$singleUser, array|false $singleUser,
array $users = [], array $users = [],
$shareeEnumerationPhone = false, bool $shareeEnumerationPhone = false,
): void { ): void {
if ($singleUser !== false) {
$singleUser = $this->getUserMock(...$singleUser);
}
$users = array_map(
fn ($args) => [$args[0], $this->getUserMock(...$args[1])],
$users
);
$this->mockConfig(['core' => [ $this->mockConfig(['core' => [
'shareapi_only_share_with_group_members' => $shareWithGroupOnly ? 'yes' : 'no', 'shareapi_only_share_with_group_members' => $shareWithGroupOnly ? 'yes' : 'no',
'shareapi_allow_share_dialog_user_enumeration' => $shareeEnumeration? 'yes' : 'no', 'shareapi_allow_share_dialog_user_enumeration' => $shareeEnumeration? 'yes' : 'no',
@ -458,6 +431,10 @@ class UserPluginTest extends TestCase {
->willReturn($this->user); ->willReturn($this->user);
if (!$shareWithGroupOnly) { if (!$shareWithGroupOnly) {
$userResponse = array_map(
fn ($args) => $this->getUserMock(...$args),
$userResponse
);
if ($shareeEnumerationPhone) { if ($shareeEnumerationPhone) {
$this->userManager->expects($this->once()) $this->userManager->expects($this->once())
->method('searchKnownUsersByDisplayName') ->method('searchKnownUsersByDisplayName')
@ -534,13 +511,8 @@ class UserPluginTest extends TestCase {
]; ];
} }
/** #[DataProvider('takeOutCurrentUserProvider')]
* @param array $users public function testTakeOutCurrentUser(array $users, array $expectedUIDs, ?string $currentUserId): void {
* @param array $expectedUIDs
* @param $currentUserId
*/
#[\PHPUnit\Framework\Attributes\DataProvider('takeOutCurrentUserProvider')]
public function testTakeOutCurrentUser(array $users, array $expectedUIDs, $currentUserId): void {
$this->instantiatePlugin(); $this->instantiatePlugin();
$this->session->expects($this->once()) $this->session->expects($this->once())
@ -717,7 +689,7 @@ class UserPluginTest extends TestCase {
]; ];
} }
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchEnumeration')] #[DataProvider('dataSearchEnumeration')]
public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result, $mockedSettings): void { public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result, $mockedSettings): void {
$this->mockConfig($mockedSettings); $this->mockConfig($mockedSettings);

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@ -15,11 +17,11 @@ use OC\DB\QueryBuilder\Parameter;
use OC\DB\QueryBuilder\QueryBuilder; use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig; use OC\SystemConfig;
use OCP\DB\IResult; use OCP\DB\IResult;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Server; use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -30,17 +32,11 @@ use Psr\Log\LoggerInterface;
* @package Test\DB\QueryBuilder * @package Test\DB\QueryBuilder
*/ */
class QueryBuilderTest extends \Test\TestCase { class QueryBuilderTest extends \Test\TestCase {
/** @var QueryBuilder */ private SystemConfig&MockObject $config;
protected $queryBuilder; private LoggerInterface&MockObject $logger;
/** @var IDBConnection */
protected $connection;
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */ private QueryBuilder $queryBuilder;
protected $config; private IDBConnection $connection;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -105,7 +101,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param int|null $firstResult * @param int|null $firstResult
* @param array $expectedSet * @param array $expectedSet
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataFirstResult')] #[DataProvider('dataFirstResult')]
public function testFirstResult($firstResult, $expectedSet): void { public function testFirstResult($firstResult, $expectedSet): void {
$this->deleteTestingRows(); $this->deleteTestingRows();
$this->createTestingRows(); $this->createTestingRows();
@ -142,7 +138,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param int $maxResult * @param int $maxResult
* @param array $expectedSet * @param array $expectedSet
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataMaxResults')] #[DataProvider('dataMaxResults')]
public function testMaxResults($maxResult, $expectedSet): void { public function testMaxResults($maxResult, $expectedSet): void {
$this->deleteTestingRows(); $this->deleteTestingRows();
$this->createTestingRows(); $this->createTestingRows();
@ -164,10 +160,7 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows(); $this->deleteTestingRows();
} }
public function dataSelect(): array { public static function dataSelect(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
return [ return [
// select('column1') // select('column1')
[['configvalue'], ['configvalue' => '99']], [['configvalue'], ['configvalue' => '99']],
@ -179,31 +172,30 @@ class QueryBuilderTest extends \Test\TestCase {
[[['configvalue', 'configkey']], ['configvalue' => '99', 'configkey' => 'testing1']], [[['configvalue', 'configkey']], ['configvalue' => '99', 'configkey' => 'testing1']],
// select(new Literal('column1')) // select(new Literal('column1'))
[[$queryBuilder->expr()->literal('column1')], [], 'column1'], [['l::column1'], [], 'column1'],
// select('column1', 'column2') // select(new Literal('column1'), 'column2')
[[$queryBuilder->expr()->literal('column1'), 'configkey'], ['configkey' => 'testing1'], 'column1'], [['l::column1', 'configkey'], ['configkey' => 'testing1'], 'column1'],
// select(['column1', 'column2']) // select([new Literal('column1'), 'column2'])
[[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['configkey' => 'testing1'], 'column1'], [[['l::column1', 'configkey']], ['configkey' => 'testing1'], 'column1'],
]; ];
} }
/** #[DataProvider('dataSelect')]
* public function testSelect(array $selectArguments, array $expected, string $expectedLiteral = ''): void {
* @param array $selectArguments
* @param array $expected
* @param string $expectedLiteral
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSelect')]
public function testSelect($selectArguments, $expected, $expectedLiteral = ''): void {
$this->deleteTestingRows(); $this->deleteTestingRows();
$this->createTestingRows(); $this->createTestingRows();
call_user_func_array( array_walk_recursive(
[$this->queryBuilder, 'select'], $selectArguments,
$selectArguments function (string &$arg) {
if (\str_starts_with($arg, 'l::')) {
$arg = $this->queryBuilder->expr()->literal(substr($arg, 3));
}
},
); );
$this->queryBuilder->select(...$selectArguments);
$this->queryBuilder->from('*PREFIX*appconfig') $this->queryBuilder->from('*PREFIX*appconfig')
->where($this->queryBuilder->expr()->eq( ->where($this->queryBuilder->expr()->eq(
@ -232,18 +224,19 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows(); $this->deleteTestingRows();
} }
public function dataSelectAlias(): array { public static function dataSelectAlias(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
return [ return [
['configvalue', 'cv', ['cv' => '99']], ['configvalue', 'cv', ['cv' => '99']],
[$queryBuilder->expr()->literal('column1'), 'thing', ['thing' => 'column1']], ['l::column1', 'thing', ['thing' => 'column1']],
]; ];
} }
#[\PHPUnit\Framework\Attributes\DataProvider('dataSelectAlias')] #[DataProvider('dataSelectAlias')]
public function testSelectAlias(string|ILiteral $select, string $alias, array $expected): void { public function testSelectAlias(string $select, string $alias, array $expected): void {
if (str_starts_with($select, 'l::')) {
$select = $this->queryBuilder->expr()->literal(substr($select, 3));
}
$this->deleteTestingRows(); $this->deleteTestingRows();
$this->createTestingRows(); $this->createTestingRows();
@ -335,10 +328,7 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows('testFirstResult2'); $this->deleteTestingRows('testFirstResult2');
} }
public function dataAddSelect(): array { public static function dataAddSelect(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
return [ return [
// addSelect('column1') // addSelect('column1')
[['configvalue'], ['appid' => 'testFirstResult', 'configvalue' => '99']], [['configvalue'], ['appid' => 'testFirstResult', 'configvalue' => '99']],
@ -350,27 +340,30 @@ class QueryBuilderTest extends \Test\TestCase {
[[['configvalue', 'configkey']], ['appid' => 'testFirstResult', 'configvalue' => '99', 'configkey' => 'testing1']], [[['configvalue', 'configkey']], ['appid' => 'testFirstResult', 'configvalue' => '99', 'configkey' => 'testing1']],
// select(new Literal('column1')) // select(new Literal('column1'))
[[$queryBuilder->expr()->literal('column1')], ['appid' => 'testFirstResult'], 'column1'], [['l::column1'], ['appid' => 'testFirstResult'], 'column1'],
// select('column1', 'column2') // select(new Literal('column1'), 'column2')
[[$queryBuilder->expr()->literal('column1'), 'configkey'], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'], [['l::column1', 'configkey'], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
// select(['column1', 'column2']) // select([new Literal('column1'), 'column2'])
[[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'], [[['l::column1', 'configkey']], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
]; ];
} }
/** #[DataProvider('dataAddSelect')]
* public function testAddSelect(array $selectArguments, array $expected, string $expectedLiteral = ''): void {
* @param array $selectArguments
* @param array $expected
* @param string $expectedLiteral
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddSelect')]
public function testAddSelect($selectArguments, $expected, $expectedLiteral = ''): void {
$this->deleteTestingRows(); $this->deleteTestingRows();
$this->createTestingRows(); $this->createTestingRows();
array_walk_recursive(
$selectArguments,
function (string &$arg) {
if (\str_starts_with($arg, 'l::')) {
$arg = $this->queryBuilder->expr()->literal(substr($arg, 3));
}
},
);
$this->queryBuilder->select('appid'); $this->queryBuilder->select('appid');
call_user_func_array( call_user_func_array(
@ -419,7 +412,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataDelete')] #[DataProvider('dataDelete')]
public function testDelete($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void { public function testDelete($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->delete($tableName, $tableAlias); $this->queryBuilder->delete($tableName, $tableAlias);
@ -448,7 +441,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataUpdate')] #[DataProvider('dataUpdate')]
public function testUpdate($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void { public function testUpdate($tableName, $tableAlias, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->update($tableName, $tableAlias); $this->queryBuilder->update($tableName, $tableAlias);
@ -475,7 +468,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataInsert')] #[DataProvider('dataInsert')]
public function testInsert($tableName, $expectedQueryPart, $expectedQuery): void { public function testInsert($tableName, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert($tableName); $this->queryBuilder->insert($tableName);
@ -490,12 +483,9 @@ class QueryBuilderTest extends \Test\TestCase {
); );
} }
public function dataFrom(): array { public static function dataFrom(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$qb = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
return [ return [
[$qb->createFunction('(' . $qb->select('*')->from('test')->getSQL() . ')'), 'q', null, null, [ ['function', 'q', null, null, [
['table' => '(SELECT * FROM `*PREFIX*test`)', 'alias' => '`q`'] ['table' => '(SELECT * FROM `*PREFIX*test`)', 'alias' => '`q`']
], '(SELECT * FROM `*PREFIX*test`) `q`'], ], '(SELECT * FROM `*PREFIX*test`) `q`'],
['data', null, null, null, [['table' => '`*PREFIX*data`', 'alias' => null]], '`*PREFIX*data`'], ['data', null, null, null, [['table' => '`*PREFIX*data`', 'alias' => null]], '`*PREFIX*data`'],
@ -511,17 +501,15 @@ class QueryBuilderTest extends \Test\TestCase {
]; ];
} }
/** #[DataProvider('dataFrom')]
* public function testFrom(string $table1Name, ?string $table1Alias, ?string $table2Name, ?string $table2Alias, array $expectedQueryPart, string $expectedQuery): void {
* @param string|IQueryFunction $table1Name $config = $this->createMock(SystemConfig::class);
* @param string $table1Alias $logger = $this->createMock(LoggerInterface::class);
* @param string|IQueryFunction $table2Name $queryBuilder = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
* @param string $table2Alias
* @param array $expectedQueryPart if ($table1Name === 'function') {
* @param string $expectedQuery $table1Name = $queryBuilder->createFunction('(' . $queryBuilder->select('*')->from('test')->getSQL() . ')');
*/ }
#[\PHPUnit\Framework\Attributes\DataProvider('dataFrom')]
public function testFrom($table1Name, $table1Alias, $table2Name, $table2Alias, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from($table1Name, $table1Alias); $this->queryBuilder->from($table1Name, $table1Alias);
if ($table2Name !== null) { if ($table2Name !== null) {
$this->queryBuilder->from($table2Name, $table2Alias); $this->queryBuilder->from($table2Name, $table2Alias);
@ -568,7 +556,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataJoin')] #[DataProvider('dataJoin')]
public function testJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void { public function testJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1'); $this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->join( $this->queryBuilder->join(
@ -598,7 +586,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataJoin')] #[DataProvider('dataJoin')]
public function testInnerJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void { public function testInnerJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1'); $this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->innerJoin( $this->queryBuilder->innerJoin(
@ -648,7 +636,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataLeftJoin')] #[DataProvider('dataLeftJoin')]
public function testLeftJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void { public function testLeftJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1'); $this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->leftJoin( $this->queryBuilder->leftJoin(
@ -698,7 +686,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataRightJoin')] #[DataProvider('dataRightJoin')]
public function testRightJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void { public function testRightJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->from('data1', 'd1'); $this->queryBuilder->from('data1', 'd1');
$this->queryBuilder->rightJoin( $this->queryBuilder->rightJoin(
@ -737,7 +725,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataSet')] #[DataProvider('dataSet')]
public function testSet($partOne1, $partOne2, $partTwo1, $partTwo2, $expectedQueryPart, $expectedQuery): void { public function testSet($partOne1, $partOne2, $partTwo1, $partTwo2, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->update('data'); $this->queryBuilder->update('data');
$this->queryBuilder->set($partOne1, $partOne2); $this->queryBuilder->set($partOne1, $partOne2);
@ -769,7 +757,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataWhere')] #[DataProvider('dataWhere')]
public function testWhere($whereArguments, $expectedQueryPart, $expectedQuery): void { public function testWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column'); $this->queryBuilder->select('column');
call_user_func_array( call_user_func_array(
@ -794,7 +782,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataWhere')] #[DataProvider('dataWhere')]
public function testAndWhere($whereArguments, $expectedQueryPart, $expectedQuery): void { public function testAndWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column'); $this->queryBuilder->select('column');
call_user_func_array( call_user_func_array(
@ -826,7 +814,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrWhere')] #[DataProvider('dataOrWhere')]
public function testOrWhere($whereArguments, $expectedQueryPart, $expectedQuery): void { public function testOrWhere($whereArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column'); $this->queryBuilder->select('column');
call_user_func_array( call_user_func_array(
@ -858,7 +846,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataGroupBy')] #[DataProvider('dataGroupBy')]
public function testGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void { public function testGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column'); $this->queryBuilder->select('column');
call_user_func_array( call_user_func_array(
@ -890,7 +878,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddGroupBy')] #[DataProvider('dataAddGroupBy')]
public function testAddGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void { public function testAddGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->select('column'); $this->queryBuilder->select('column');
$this->queryBuilder->groupBy('column1'); $this->queryBuilder->groupBy('column1');
@ -923,7 +911,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')] #[DataProvider('dataSetValue')]
public function testSetValue($column, $value, $expectedQueryPart, $expectedQuery): void { public function testSetValue($column, $value, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert('data'); $this->queryBuilder->insert('data');
$this->queryBuilder->setValue($column, $value); $this->queryBuilder->setValue($column, $value);
@ -946,7 +934,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')] #[DataProvider('dataSetValue')]
public function testValues($column, $value, $expectedQueryPart, $expectedQuery): void { public function testValues($column, $value, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->insert('data'); $this->queryBuilder->insert('data');
$this->queryBuilder->values([ $this->queryBuilder->values([
@ -987,7 +975,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataHaving')] #[DataProvider('dataHaving')]
public function testHaving($havingArguments, $expectedQueryPart, $expectedQuery): void { public function testHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
call_user_func_array( call_user_func_array(
[$this->queryBuilder, 'having'], [$this->queryBuilder, 'having'],
@ -1028,7 +1016,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataAndHaving')] #[DataProvider('dataAndHaving')]
public function testAndHaving($havingArguments, $expectedQueryPart, $expectedQuery): void { public function testAndHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->having('condition1'); $this->queryBuilder->having('condition1');
call_user_func_array( call_user_func_array(
@ -1070,7 +1058,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrHaving')] #[DataProvider('dataOrHaving')]
public function testOrHaving($havingArguments, $expectedQueryPart, $expectedQuery): void { public function testOrHaving($havingArguments, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->having('condition1'); $this->queryBuilder->having('condition1');
call_user_func_array( call_user_func_array(
@ -1104,7 +1092,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataOrderBy')] #[DataProvider('dataOrderBy')]
public function testOrderBy($sort, $order, $expectedQueryPart, $expectedQuery): void { public function testOrderBy($sort, $order, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->orderBy($sort, $order); $this->queryBuilder->orderBy($sort, $order);
@ -1141,7 +1129,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param array $expectedQueryPart * @param array $expectedQueryPart
* @param string $expectedQuery * @param string $expectedQuery
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataAddOrderBy')] #[DataProvider('dataAddOrderBy')]
public function testAddOrderBy($sort2, $order2, $order1, $expectedQueryPart, $expectedQuery): void { public function testAddOrderBy($sort2, $order2, $order1, $expectedQueryPart, $expectedQuery): void {
$this->queryBuilder->orderBy('column1', $order1); $this->queryBuilder->orderBy('column1', $order1);
$this->queryBuilder->addOrderBy($sort2, $order2); $this->queryBuilder->addOrderBy($sort2, $order2);
@ -1194,10 +1182,7 @@ class QueryBuilderTest extends \Test\TestCase {
} }
} }
public function dataGetTableName(): array { public static function dataGetTableName(): array {
$config = $this->createMock(SystemConfig::class);
$logger = $this->createMock(LoggerInterface::class);
$qb = new QueryBuilder(Server::get(IDBConnection::class), $config, $logger);
return [ return [
['*PREFIX*table', null, '`*PREFIX*table`'], ['*PREFIX*table', null, '`*PREFIX*table`'],
['*PREFIX*table', true, '`*PREFIX*table`'], ['*PREFIX*table', true, '`*PREFIX*table`'],
@ -1207,20 +1192,18 @@ class QueryBuilderTest extends \Test\TestCase {
['table', true, '`*PREFIX*table`'], ['table', true, '`*PREFIX*table`'],
['table', false, '`table`'], ['table', false, '`table`'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), null, '(SELECT * FROM `*PREFIX*table`)'], ['function', null, '(SELECT * FROM `*PREFIX*table`)'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), true, '(SELECT * FROM `*PREFIX*table`)'], ['function', true, '(SELECT * FROM `*PREFIX*table`)'],
[$qb->createFunction('(' . $qb->select('*')->from('table')->getSQL() . ')'), false, '(SELECT * FROM `*PREFIX*table`)'], ['function', false, '(SELECT * FROM `*PREFIX*table`)'],
]; ];
} }
/** #[DataProvider('dataGetTableName')]
* public function testGetTableName(string $tableName, ?bool $automatic, string $expected): void {
* @param string|IQueryFunction $tableName if ($tableName === 'function') {
* @param bool $automatic $tableName = $this->queryBuilder->createFunction('(' . $this->queryBuilder->select('*')->from('table')->getSQL() . ')');
* @param string $expected }
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetTableName')]
public function testGetTableName($tableName, $automatic, $expected): void {
if ($automatic !== null) { if ($automatic !== null) {
$this->queryBuilder->automaticTablePrefix($automatic); $this->queryBuilder->automaticTablePrefix($automatic);
} }
@ -1243,7 +1226,7 @@ class QueryBuilderTest extends \Test\TestCase {
* @param string $prefix * @param string $prefix
* @param string $expected * @param string $expected
*/ */
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetColumnName')] #[DataProvider('dataGetColumnName')]
public function testGetColumnName($column, $prefix, $expected): void { public function testGetColumnName($column, $prefix, $expected): void {
$this->assertSame( $this->assertSame(
$expected, $expected,

Loading…
Cancel
Save