|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace theme_boost_union; |
| 18 | + |
| 19 | +use advanced_testcase; |
| 20 | +use theme_boost_union\snippets; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for Boost Union SCSS Snippets. |
| 24 | + * |
| 25 | + * @package theme_boost_union |
| 26 | + * @category test |
| 27 | + * @coversDefaultClass \theme_boost_union\snippets |
| 28 | + * @copyright André Menrath <[email protected]>, 2024 University of Graz |
| 29 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 30 | + */ |
| 31 | +final class snippets_test extends advanced_testcase { |
| 32 | + /** |
| 33 | + * Test the parsing of the snippets meta information from the files top comment. |
| 34 | + * |
| 35 | + * @covers ::get_snippet_meta |
| 36 | + * @covers ::get_snippet_meta_from_file |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function test_parse_snippet_header(): void { |
| 41 | + $meta = snippets::get_snippet_meta('visual-depth.scss', 'theme_boost_union'); |
| 42 | + $this->assertEquals('Visual depth', $meta->title); |
| 43 | + $this->assertEquals('global', $meta->scope); |
| 44 | + $this->assertEquals('eyecandy', $meta->goal); |
| 45 | + $this->assertEquals( |
| 46 | + 'A less flat design than is intended in the Boost theme. Realised by ' . |
| 47 | + 'box-shadows on a number of page elements and a colour gradient on the page background.', |
| 48 | + $meta->description |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Register (new) local builtin snippets in the database. |
| 54 | + * |
| 55 | + * @covers ::add_builtin_snippets |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function test_register_builtin_snippets(): void { |
| 60 | + global $DB; |
| 61 | + |
| 62 | + $this->resetAfterTest(); |
| 63 | + |
| 64 | + $countinitial = $DB->count_records( |
| 65 | + 'theme_boost_union_snippets', |
| 66 | + ['source' => 'theme_boost_union'] |
| 67 | + ); |
| 68 | + |
| 69 | + // Run function that parses builtin snippets. |
| 70 | + snippets::add_builtin_snippets(); |
| 71 | + |
| 72 | + $count = $DB->count_records( |
| 73 | + 'theme_boost_union_snippets', |
| 74 | + ['source' => 'theme_boost_union'] |
| 75 | + ); |
| 76 | + |
| 77 | + // Since we have added no snippets in between the count should still be the same. |
| 78 | + $this->assertEquals($count, $countinitial); |
| 79 | + |
| 80 | + // Delete one snippet from the database. |
| 81 | + $DB->delete_records( |
| 82 | + 'theme_boost_union_snippets', |
| 83 | + ['source' => 'theme_boost_union', 'path' => 'visual-depth.scss'] |
| 84 | + ); |
| 85 | + |
| 86 | + $count = $DB->count_records( |
| 87 | + 'theme_boost_union_snippets', |
| 88 | + ['source' => 'theme_boost_union'] |
| 89 | + ); |
| 90 | + |
| 91 | + $this->assertEquals($countinitial - 1, $count); |
| 92 | + |
| 93 | + // Run function that parses and registers new builtin snippets. |
| 94 | + snippets::add_builtin_snippets(); |
| 95 | + |
| 96 | + // The builtin snippet which was just deleted from the db should be registered again. |
| 97 | + $snippet = $DB->get_record( |
| 98 | + 'theme_boost_union_snippets', |
| 99 | + ['source' => 'theme_boost_union', 'path' => 'visual-depth.scss'] |
| 100 | + ); |
| 101 | + |
| 102 | + $this->assertNotEmpty($snippet); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test the parsing snippet which is not present/readable. |
| 107 | + * |
| 108 | + * @covers ::get_snippet_meta |
| 109 | + * @covers ::get_snippet_meta_from_file |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function test_parse_snippet_header_of_non_existing_snippet(): void { |
| 114 | + $meta = snippets::get_snippet_meta('this_snippet_does_not_exist.scss', 'theme_boost_union'); |
| 115 | + $this->assertEquals(null, $meta); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test the parsing snippet which is not present/readable. |
| 120 | + * |
| 121 | + * @covers ::get_snippet_meta |
| 122 | + * @covers ::get_snippet_meta_from_file |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function test_fetch_enabled_snippets_scss(): void { |
| 127 | + global $DB; |
| 128 | + |
| 129 | + $this->resetAfterTest(); |
| 130 | + |
| 131 | + $scss = snippets::get_enabled_snippet_scss(); |
| 132 | + |
| 133 | + // No snippets are enabled by default, so the scss should be empty. |
| 134 | + $this->assertEquals('', $scss); |
| 135 | + |
| 136 | + // Enable a snippet directly via the DB. |
| 137 | + $snippet = $DB->get_record( |
| 138 | + 'theme_boost_union_snippets', |
| 139 | + ['source' => 'theme_boost_union', 'path' => 'visual-depth.scss'] |
| 140 | + ); |
| 141 | + $snippet->enabled = 1; |
| 142 | + $DB->update_record('theme_boost_union_snippets', $snippet); |
| 143 | + |
| 144 | + $scss = snippets::get_enabled_snippet_scss(); |
| 145 | + |
| 146 | + // Verify that the Snippets SCSS content is now queried. |
| 147 | + $this->assertNotEquals('', $scss); |
| 148 | + $this->assertStringContainsString('Snippet Title: Visual depth', $scss); |
| 149 | + } |
| 150 | +} |
0 commit comments