From 157f8a87b4f051ecdfeef03e186868d6ddff70c7 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Mon, 20 Jul 2020 12:41:24 +0200 Subject: [PATCH] MDL-69089 core_contentbank: Tests for empty content names --- contentbank/tests/content_test.php | 4 +++- contentbank/tests/contenttype_test.php | 20 +++++++++------- .../tests/external/rename_content_test.php | 24 ++++++++++--------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/contentbank/tests/content_test.php b/contentbank/tests/content_test.php index b0bfede8218..ddfff9422ae 100644 --- a/contentbank/tests/content_test.php +++ b/contentbank/tests/content_test.php @@ -81,7 +81,9 @@ class core_contenttype_content_testcase extends \advanced_testcase { 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'], 'Name with tags' => ['This is bold', 'This is bold'], 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)], - 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)] + 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)], + 'Empty name' => ['', 'Old name'], + 'Blanks only' => [' ', 'Old name'], ]; } diff --git a/contentbank/tests/contenttype_test.php b/contentbank/tests/contenttype_test.php index 1fee5be6a31..c8ae908696e 100644 --- a/contentbank/tests/contenttype_test.php +++ b/contentbank/tests/contenttype_test.php @@ -375,12 +375,14 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase { */ public function rename_content_provider() { return [ - 'Standard name' => ['New name', 'New name'], - 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'], - 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'], - 'Name with tags' => ['This is bold', 'This is bold'], - 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)], - 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)] + 'Standard name' => ['New name', 'New name', true], + 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true], + 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true], + 'Name with tags' => ['This is bold', 'This is bold', true], + 'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true], + 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true], + 'Empty name' => ['', 'Test content ', false], + 'Blanks only' => [' ', 'Test content ', false], ]; } @@ -390,10 +392,11 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase { * @dataProvider rename_content_provider * @param string $newname The name to set * @param string $expected The name result + * @param bool $result The bolean result expected when renaming * * @covers ::rename_content */ - public function test_rename_content(string $newname, string $expected) { + public function test_rename_content(string $newname, string $expected, bool $result) { global $DB; $this->resetAfterTest(); @@ -414,9 +417,8 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase { // Check the content is renamed as expected by a user with permission. $renamed = $contenttype->rename_content($content, $newname); - $this->assertTrue($renamed); + $this->assertEquals($result, $renamed); $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]); - $this->assertNotEquals($oldname, $record->name); $this->assertEquals($expected, $record->name); } diff --git a/contentbank/tests/external/rename_content_test.php b/contentbank/tests/external/rename_content_test.php index 6a9ea676804..d369bb2bd9e 100644 --- a/contentbank/tests/external/rename_content_test.php +++ b/contentbank/tests/external/rename_content_test.php @@ -52,12 +52,14 @@ class rename_content_testcase extends \externallib_advanced_testcase { */ public function rename_content_provider() { return [ - 'Standard name' => ['New name', 'New name'], - 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'], - 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'], - 'Name with tags' => ['This is bold', 'This is bold'], - 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)], - 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)] + 'Standard name' => ['New name', 'New name', true], + 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true], + 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true], + 'Name with tags' => ['This is bold', 'This is bold', true], + 'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true], + 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true], + 'Empty name' => ['', 'Test content ', false], + 'Blanks only' => [' ', 'Test content ', false], ]; } @@ -66,11 +68,12 @@ class rename_content_testcase extends \externallib_advanced_testcase { * * @dataProvider rename_content_provider * @param string $newname The name to set - * @param string $expected The name result + * @param string $expectedname The name result + * @param bool $expectedresult The bolean result expected when renaming * * @covers ::execute */ - public function test_rename_content_with_permission(string $newname, string $expected) { + public function test_rename_content_with_permission(string $newname, string $expectedname, bool $expectedresult) { global $DB; $this->resetAfterTest(); @@ -91,10 +94,9 @@ class rename_content_testcase extends \externallib_advanced_testcase { // Call the WS and check the content is renamed as expected. $result = rename_content::execute($content->get_id(), $newname); $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result); - $this->assertTrue($result['result']); + $this->assertEquals($expectedresult, $result['result']); $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]); - $this->assertNotEquals($oldname, $record->name); - $this->assertEquals($expected, $record->name); + $this->assertEquals($expectedname, $record->name); // Call the WS using an unexisting contentid and check an error is thrown. $this->expectException(\invalid_response_exception::class);