From 3904acfe932be26be71daf2ae047a144ac8b48c9 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 23 Jul 2020 13:36:32 +0200 Subject: [PATCH 1/5] Use assertStringContainsString instead of assertContains on strings Signed-off-by: Morris Jobke --- apps/dav/tests/unit/Command/ListCalendarsTest.php | 9 ++++----- apps/dav/tests/unit/Command/MoveCalendarTest.php | 7 +++---- tests/Core/Command/TwoFactorAuth/CleanupTest.php | 2 +- tests/Core/Command/TwoFactorAuth/DisableTest.php | 6 +++--- tests/Core/Command/TwoFactorAuth/EnableTest.php | 6 +++--- tests/Core/Command/TwoFactorAuth/EnforceTest.php | 12 ++++++------ tests/Core/Command/TwoFactorAuth/StateTest.php | 6 +++--- tests/lib/AppFramework/Http/DownloadResponseTest.php | 4 ++-- 8 files changed, 25 insertions(+), 27 deletions(-) diff --git a/apps/dav/tests/unit/Command/ListCalendarsTest.php b/apps/dav/tests/unit/Command/ListCalendarsTest.php index 42e24be5cd3..9a6723860b9 100644 --- a/apps/dav/tests/unit/Command/ListCalendarsTest.php +++ b/apps/dav/tests/unit/Command/ListCalendarsTest.php @@ -63,7 +63,6 @@ class ListCalendarsTest extends TestCase { ); } - public function testWithBadUser() { $this->expectException(\InvalidArgumentException::class); @@ -77,7 +76,7 @@ class ListCalendarsTest extends TestCase { $commandTester->execute([ 'uid' => self::USERNAME, ]); - $this->assertContains("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay()); + $this->assertStringContainsString("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay()); } public function testWithCorrectUserWithNoCalendars() @@ -96,7 +95,7 @@ class ListCalendarsTest extends TestCase { $commandTester->execute([ 'uid' => self::USERNAME, ]); - $this->assertContains("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay()); + $this->assertStringContainsString("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay()); } public function dataExecute() @@ -137,7 +136,7 @@ class ListCalendarsTest extends TestCase { $commandTester->execute([ 'uid' => self::USERNAME, ]); - $this->assertContains($output, $commandTester->getDisplay()); - $this->assertNotContains(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay()); + $this->assertStringContainsString($output, $commandTester->getDisplay()); + $this->assertStringNotContainsString(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay()); } } diff --git a/apps/dav/tests/unit/Command/MoveCalendarTest.php b/apps/dav/tests/unit/Command/MoveCalendarTest.php index e286c4f292a..c537fcb63e7 100644 --- a/apps/dav/tests/unit/Command/MoveCalendarTest.php +++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php @@ -121,7 +121,6 @@ class MoveCalendarTest extends TestCase { ]); } - public function testMoveWithInexistantCalendar() { $this->expectException(\InvalidArgumentException::class); @@ -149,7 +148,7 @@ class MoveCalendarTest extends TestCase { ]); } - + public function testMoveWithExistingDestinationCalendar() { $this->expectException(\InvalidArgumentException::class); @@ -319,7 +318,7 @@ class MoveCalendarTest extends TestCase { 'destinationuid' => 'user2', ]); - $this->assertContains("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); + $this->assertStringContainsString("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); } public function testMoveWithDestinationNotPartOfGroupAndForce() @@ -367,7 +366,7 @@ class MoveCalendarTest extends TestCase { '--force' => true ]); - $this->assertContains("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); + $this->assertStringContainsString("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); } public function dataTestMoveWithCalendarAlreadySharedToDestination(): array diff --git a/tests/Core/Command/TwoFactorAuth/CleanupTest.php b/tests/Core/Command/TwoFactorAuth/CleanupTest.php index 24aaa9abce2..6281c2e4c5f 100644 --- a/tests/Core/Command/TwoFactorAuth/CleanupTest.php +++ b/tests/Core/Command/TwoFactorAuth/CleanupTest.php @@ -60,7 +60,7 @@ class CleanupTest extends TestCase { $this->assertEquals(0, $rc); $output = $this->cmd->getDisplay(); - $this->assertContains("All user-provider associations for provider u2f have been removed", $output); + $this->assertStringContainsString("All user-provider associations for provider u2f have been removed", $output); } } diff --git a/tests/Core/Command/TwoFactorAuth/DisableTest.php b/tests/Core/Command/TwoFactorAuth/DisableTest.php index fc0def50b90..5accaccb907 100644 --- a/tests/Core/Command/TwoFactorAuth/DisableTest.php +++ b/tests/Core/Command/TwoFactorAuth/DisableTest.php @@ -67,7 +67,7 @@ class DisableTest extends TestCase { ]); $this->assertEquals(1, $rc); - $this->assertContains("Invalid UID", $this->command->getDisplay()); + $this->assertStringContainsString("Invalid UID", $this->command->getDisplay()); } public function testEnableNotSupported() { @@ -87,7 +87,7 @@ class DisableTest extends TestCase { ]); $this->assertEquals(2, $rc); - $this->assertContains("The provider does not support this operation", $this->command->getDisplay()); + $this->assertStringContainsString("The provider does not support this operation", $this->command->getDisplay()); } public function testEnabled() { @@ -107,6 +107,6 @@ class DisableTest extends TestCase { ]); $this->assertEquals(0, $rc); - $this->assertContains("Two-factor provider totp disabled for user ricky", $this->command->getDisplay()); + $this->assertStringContainsString("Two-factor provider totp disabled for user ricky", $this->command->getDisplay()); } } diff --git a/tests/Core/Command/TwoFactorAuth/EnableTest.php b/tests/Core/Command/TwoFactorAuth/EnableTest.php index faf00ed1ded..af7140e6e75 100644 --- a/tests/Core/Command/TwoFactorAuth/EnableTest.php +++ b/tests/Core/Command/TwoFactorAuth/EnableTest.php @@ -67,7 +67,7 @@ class EnableTest extends TestCase { ]); $this->assertEquals(1, $rc); - $this->assertContains("Invalid UID", $this->command->getDisplay()); + $this->assertStringContainsString("Invalid UID", $this->command->getDisplay()); } public function testEnableNotSupported() { @@ -87,7 +87,7 @@ class EnableTest extends TestCase { ]); $this->assertEquals(2, $rc); - $this->assertContains("The provider does not support this operation", $this->command->getDisplay()); + $this->assertStringContainsString("The provider does not support this operation", $this->command->getDisplay()); } public function testEnabled() { @@ -107,7 +107,7 @@ class EnableTest extends TestCase { ]); $this->assertEquals(0, $rc); - $this->assertContains("Two-factor provider totp enabled for user belle", $this->command->getDisplay()); + $this->assertStringContainsString("Two-factor provider totp enabled for user belle", $this->command->getDisplay()); } } diff --git a/tests/Core/Command/TwoFactorAuth/EnforceTest.php b/tests/Core/Command/TwoFactorAuth/EnforceTest.php index aa2cbc10620..744ea9e3b5d 100644 --- a/tests/Core/Command/TwoFactorAuth/EnforceTest.php +++ b/tests/Core/Command/TwoFactorAuth/EnforceTest.php @@ -64,7 +64,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is enforced for all users", $display); + $this->assertStringContainsString("Two-factor authentication is enforced for all users", $display); } public function testEnforceForOneGroup() { @@ -82,7 +82,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is enforced for members of the group(s) twofactorers", $display); + $this->assertStringContainsString("Two-factor authentication is enforced for members of the group(s) twofactorers", $display); } public function testEnforceForAllExceptOneGroup() { @@ -100,7 +100,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is enforced for all users, except members of yoloers", $display); + $this->assertStringContainsString("Two-factor authentication is enforced for all users, except members of yoloers", $display); } public function testDisableEnforced() { @@ -117,7 +117,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is not enforced", $display); + $this->assertStringContainsString("Two-factor authentication is not enforced", $display); } public function testCurrentStateEnabled() { @@ -129,7 +129,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is enforced for all users", $display); + $this->assertStringContainsString("Two-factor authentication is enforced for all users", $display); } public function testCurrentStateDisabled() { @@ -141,7 +141,7 @@ class EnforceTest extends TestCase { $this->assertEquals(0, $rc); $display = $this->command->getDisplay(); - $this->assertContains("Two-factor authentication is not enforced", $display); + $this->assertStringContainsString("Two-factor authentication is not enforced", $display); } } diff --git a/tests/Core/Command/TwoFactorAuth/StateTest.php b/tests/Core/Command/TwoFactorAuth/StateTest.php index 8d1b28862d3..2de374dc6d0 100644 --- a/tests/Core/Command/TwoFactorAuth/StateTest.php +++ b/tests/Core/Command/TwoFactorAuth/StateTest.php @@ -61,7 +61,7 @@ class StateTest extends TestCase { ]); $output = $this->cmd->getDisplay(); - $this->assertContains("Invalid UID", $output); + $this->assertStringContainsString("Invalid UID", $output); } public function testStateNoProvidersActive() { @@ -84,7 +84,7 @@ class StateTest extends TestCase { ]); $output = $this->cmd->getDisplay(); - $this->assertContains("Two-factor authentication is not enabled for user eldora", $output); + $this->assertStringContainsString("Two-factor authentication is not enabled for user eldora", $output); } public function testStateOneProviderActive() { @@ -107,7 +107,7 @@ class StateTest extends TestCase { ]); $output = $this->cmd->getDisplay(); - $this->assertContains("Two-factor authentication is enabled for user mohamed", $output); + $this->assertStringContainsString("Two-factor authentication is enabled for user mohamed", $output); } } diff --git a/tests/lib/AppFramework/Http/DownloadResponseTest.php b/tests/lib/AppFramework/Http/DownloadResponseTest.php index 5f816eaeb76..f1beacf5253 100644 --- a/tests/lib/AppFramework/Http/DownloadResponseTest.php +++ b/tests/lib/AppFramework/Http/DownloadResponseTest.php @@ -47,8 +47,8 @@ class DownloadResponseTest extends \Test\TestCase { public function testHeaders() { $headers = $this->response->getHeaders(); - $this->assertContains('attachment; filename="file"', $headers['Content-Disposition']); - $this->assertContains('content', $headers['Content-Type']); + $this->assertStringContainsString('attachment; filename="file"', $headers['Content-Disposition']); + $this->assertStringContainsString('content', $headers['Content-Type']); } From 7a49d0fbeadd6825cf45031e9bb9ab0e308039d5 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 23 Jul 2020 13:36:49 +0200 Subject: [PATCH 2/5] Remove deprecated test of internal attributes via assertAttributeEquals See https://github.com/sebastianbergmann/phpunit/issues/3339#issuecomment-428843322 It is seen as bad practice to test internal stuff of objects instead of the actual input and output of mathod calls. Signed-off-by: Morris Jobke --- tests/lib/ConfigTest.php | 24 +++++++++--------------- tests/lib/DB/DBSchemaTest.php | 4 ++-- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 109f7471e96..1d2440d6635 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -71,9 +71,7 @@ class ConfigTest extends TestCase { public function testSetValue() { $this->config->setValue('foo', 'moo'); - $expectedConfig = $this->initialConfig; - $expectedConfig['foo'] = 'moo'; - $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); + $this->assertSame('moo', $this->config->getValue('foo')); $content = file_get_contents($this->configFile); $expected = " 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . @@ -81,10 +79,9 @@ class ConfigTest extends TestCase { $this->assertEquals($expected, $content); $this->config->setValue('bar', 'red'); - $this->config->setValue('apps', array('files', 'gallery')); - $expectedConfig['bar'] = 'red'; - $expectedConfig['apps'] = array('files', 'gallery'); - $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); + $this->config->setValue('apps', ['files', 'gallery']); + $this->assertSame('red', $this->config->getValue('bar')); + $this->assertSame(['files', 'gallery'], $this->config->getValue('apps')); $content = file_get_contents($this->configFile); @@ -105,7 +102,8 @@ class ConfigTest extends TestCase { 'not_exists' => null, ]); - $this->assertAttributeEquals($this->initialConfig, 'cache', $this->config); + $this->assertSame('bar', $this->config->getValue('foo')); + $this->assertSame(null, $this->config->getValue('not_exists')); $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); @@ -113,10 +111,8 @@ class ConfigTest extends TestCase { 'foo' => 'moo', 'alcohol_free' => null, ]); - $expectedConfig = $this->initialConfig; - $expectedConfig['foo'] = 'moo'; - unset($expectedConfig['alcohol_free']); - $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); + $this->assertSame('moo', $this->config->getValue('foo')); + $this->assertSame(null, $this->config->getValue('not_exists')); $content = file_get_contents($this->configFile); $expected = " 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . @@ -126,9 +122,7 @@ class ConfigTest extends TestCase { public function testDeleteKey() { $this->config->deleteKey('foo'); - $expectedConfig = $this->initialConfig; - unset($expectedConfig['foo']); - $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); + $this->assertSame('this_was_clearly_not_set_before', $this->config->getValue('foo', 'this_was_clearly_not_set_before')); $content = file_get_contents($this->configFile); $expected = " \n array (\n 0 => 'Appenzeller',\n " . diff --git a/tests/lib/DB/DBSchemaTest.php b/tests/lib/DB/DBSchemaTest.php index 5fb68fdf258..d185f73e62c 100644 --- a/tests/lib/DB/DBSchemaTest.php +++ b/tests/lib/DB/DBSchemaTest.php @@ -83,8 +83,8 @@ class DBSchemaTest extends TestCase { $outfile = $this->tempManager->getTemporaryFile(); OC_DB::getDbStructure($outfile); $content = file_get_contents($outfile); - $this->assertContains($this->table1, $content); - $this->assertContains($this->table2, $content); + $this->assertStringContainsString($this->table1, $content); + $this->assertStringContainsString($this->table2, $content); } public function doTestSchemaRemoving() { From a4a086e296062c6aa7914ba6c1dddc2dddab7145 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 23 Jul 2020 13:38:49 +0200 Subject: [PATCH 3/5] Use assertEqualsCanonicalizing instead of deprecated assertEquals parameter Signed-off-by: Morris Jobke --- apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 4 ++-- tests/lib/L10N/FactoryTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 8ac4961f19f..559b6f1b417 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -280,7 +280,7 @@ EOD; $this->assertCount(0, $calendarObjects); } - + public function testMultipleCalendarObjectsWithSameUID() { $this->expectException(\Sabre\DAV\Exception\BadRequest::class); $this->expectExceptionMessage('Calendar object with uid already exists in this calendar collection.'); @@ -443,7 +443,7 @@ EOD; $expectedEventsInResult = array_map(function($index) use($events) { return $events[$index]; }, $expectedEventsInResult); - $this->assertEquals($expectedEventsInResult, $result, '', 0.0, 10, true); + $this->assertEqualsCanonicalizing($expectedEventsInResult, $result); } public function testGetCalendarObjectByUID() { diff --git a/tests/lib/L10N/FactoryTest.php b/tests/lib/L10N/FactoryTest.php index c75bfba5b99..f75eb3599f1 100644 --- a/tests/lib/L10N/FactoryTest.php +++ b/tests/lib/L10N/FactoryTest.php @@ -329,7 +329,7 @@ class FactoryTest extends TestCase { ->with($app) ->willReturn(\OC::$SERVERROOT . '/tests/data/l10n/'); - $this->assertEquals(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app), '', 0.0, 10, true); + $this->assertEqualsCanonicalizing(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app)); } public function dataLanguageExists() { @@ -360,7 +360,7 @@ class FactoryTest extends TestCase { ->with('theme') ->willReturn('abc'); - $this->assertEquals(['en', 'zz'], $factory->findAvailableLanguages($app), '', 0.0, 10, true); + $this->assertEqualsCanonicalizing(['en', 'zz'], $factory->findAvailableLanguages($app)); } /** From 67f22e28da1fe4e6c409991a4ac7e7c0cb6747c9 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 23 Jul 2020 13:39:13 +0200 Subject: [PATCH 4/5] Replace deprecated assertArraySubset with logic that does the same Signed-off-by: Morris Jobke --- .../federatedfilesharing/tests/FederatedShareProviderTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php index c8acdd3df53..379f7b14c3c 100644 --- a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php +++ b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php @@ -196,7 +196,9 @@ class FederatedShareProviderTest extends \Test\TestCase { 'accepted' => 0, 'token' => 'token', ]; - $this->assertArraySubset($expected, $data); + foreach (array_keys($expected) as $key) { + $this->assertEquals($expected[$key], $data[$key], "Assert that value for key '$key' is the same"); + } $this->assertEquals($data['id'], $share->getId()); $this->assertEquals(\OCP\Share::SHARE_TYPE_REMOTE, $share->getShareType()); From 3ab2e4d41c15252f824f989a8e23f6bb8957dfb9 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Thu, 23 Jul 2020 13:44:18 +0200 Subject: [PATCH 5/5] Remove deprecated test of internal attributes via assertAttributeEquals in constructor tests I removed the tests completely because they just test that the constructor assigns the values to the internal properties. Nothing that should be cared about from the outside. See https://github.com/sebastianbergmann/phpunit/issues/3339#issuecomment-428843322 It is seen as bad practice to test internal stuff of objects instead of the actual input and output of mathod calls. Signed-off-by: Morris Jobke --- tests/lib/Template/CSSResourceLocatorTest.php | 10 ---------- tests/lib/Template/JSResourceLocatorTest.php | 11 ----------- tests/lib/Template/ResourceLocatorTest.php | 11 ----------- 3 files changed, 32 deletions(-) diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php index 7c9f3585038..47f3347bb6f 100644 --- a/tests/lib/Template/CSSResourceLocatorTest.php +++ b/tests/lib/Template/CSSResourceLocatorTest.php @@ -107,16 +107,6 @@ class CSSResourceLocatorTest extends \Test\TestCase { return sha1(uniqid(mt_rand(), true)); } - public function testConstructor() { - $locator = $this->cssResourceLocator(); - $this->assertAttributeEquals('theme', 'theme', $locator); - $this->assertAttributeEquals('core', 'serverroot', $locator); - $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); - $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); - $this->assertAttributeEquals('map', 'webroot', $locator); - $this->assertAttributeEquals(array(), 'resources', $locator); - } - public function testFindWithAppPathSymlink() { // First create new apps path, and a symlink to it $apps_dirname = $this->randomString(); diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php index 38c5fc0eadd..e4c8a11820c 100644 --- a/tests/lib/Template/JSResourceLocatorTest.php +++ b/tests/lib/Template/JSResourceLocatorTest.php @@ -86,17 +86,6 @@ class JSResourceLocatorTest extends \Test\TestCase { return sha1(uniqid(mt_rand(), true)); } - - public function testConstructor() { - $locator = $this->jsResourceLocator(); - $this->assertAttributeEquals('theme', 'theme', $locator); - $this->assertAttributeEquals('core', 'serverroot', $locator); - $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); - $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); - $this->assertAttributeEquals('map', 'webroot', $locator); - $this->assertAttributeEquals(array(), 'resources', $locator); - } - public function testFindWithAppPathSymlink() { // First create new apps path, and a symlink to it $apps_dirname = $this->randomString(); diff --git a/tests/lib/Template/ResourceLocatorTest.php b/tests/lib/Template/ResourceLocatorTest.php index 90488071b4f..392d21ae204 100644 --- a/tests/lib/Template/ResourceLocatorTest.php +++ b/tests/lib/Template/ResourceLocatorTest.php @@ -33,17 +33,6 @@ class ResourceLocatorTest extends \Test\TestCase { '', true, true, true, array()); } - public function testConstructor() { - $locator = $this->getResourceLocator('theme', - array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); - $this->assertAttributeEquals('theme', 'theme', $locator); - $this->assertAttributeEquals('core', 'serverroot', $locator); - $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); - $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); - $this->assertAttributeEquals('map', 'webroot', $locator); - $this->assertAttributeEquals(array(), 'resources', $locator); - } - public function testFind() { $locator = $this->getResourceLocator('theme', array('core' => 'map'), array('3rd' => 'party'), array('foo' => 'bar'));