From b6bca2fd3c012d3cf52db1a03bf63fa1e42c4abd Mon Sep 17 00:00:00 2001 From: Andre Menrath Date: Tue, 10 Sep 2024 12:14:21 +0200 Subject: [PATCH] add basic unit tests for snippets library --- tests/snippets_test.php | 117 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 tests/snippets_test.php diff --git a/tests/snippets_test.php b/tests/snippets_test.php new file mode 100644 index 00000000000..1f7b16f0842 --- /dev/null +++ b/tests/snippets_test.php @@ -0,0 +1,117 @@ +. + +namespace theme_boost_union; + +use advanced_testcase; +use theme_boost_union\snippets; + +/** + * Tests for Boost Union SCSS Snippets. + * + * @package theme_boost_union + * @category test + * @coversDefaultClass \theme_boost_union\snippets + * @copyright André Menrath , 2024 University of Graz + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class snippets_test extends advanced_testcase { + /** + * Test the parsing of the snippets meta information from the files top comment. + * + * @covers ::get_snippet_meta + * @covers ::get_snippet_meta_from_file + * + * @return void + */ + public function test_parse_snippet_header(): void { + $meta = snippets::get_snippet_meta('visual-depth.scss', 'theme_boost_union'); + $this->assertEquals('Visual depth', $meta->title); + $this->assertEquals('global', $meta->scope); + $this->assertEquals('eyecandy', $meta->goal); + $this->assertEquals( + 'A less flat design than is intended in the Boost theme. Realised by ' . + 'box-shadows on a number of page elements and a colour gradient on the page background.', + $meta->description + ); + } + + /** + * Register (new) local builtin snippets in the database. + * + * @covers ::add_builtin_snippets + * + * @return void + */ + public function test_register_builtin_snippets(): void { + global $DB; + + $this->resetAfterTest(); + + $countinitial = $DB->count_records( + 'theme_boost_union_snippets', + ['source' => 'theme_boost_union'] + ); + + // Run function that parses builtin snippets. + snippets::add_builtin_snippets(); + + $count = $DB->count_records( + 'theme_boost_union_snippets', + ['source' => 'theme_boost_union'] + ); + + // Since we have added no snippets in between the count should still be the same. + $this->assertEquals($count, $countinitial); + + // Delete one snippet from the database. + $DB->delete_records( + 'theme_boost_union_snippets', + ['source' => 'theme_boost_union', 'path' => 'visual-depth.scss'] + ); + + $count = $DB->count_records( + 'theme_boost_union_snippets', + ['source' => 'theme_boost_union'] + ); + + $this->assertEquals($countinitial - 1, $count); + + // Run function that parses and registers new builtin snippets. + snippets::add_builtin_snippets(); + + // The builtin snippet which was just deleted from the db should be registered again. + $snippet = $DB->get_record( + 'theme_boost_union_snippets', + ['source' => 'theme_boost_union', 'path' => 'visual-depth.scss'] + ); + + $this->assertNotEmpty($snippet); + } + + /** + * Test the parsing snippet which is not present/readable. + * + * @covers ::get_snippet_meta + * @covers ::get_snippet_meta_from_file + * + * @return void + */ + public function test_parse_snippet_header_of_non_existing_snippet(): void { + $meta = snippets::get_snippet_meta('this_snippet_does_not_exist.scss', 'theme_boost_union'); + $this->assertEquals(null, $meta); + } +}