Skip to content

Commit 125838b

Browse files
committed
add basic unit tests for snippets library
1 parent c9d1f6f commit 125838b

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

classes/snippets.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static function compose_snippets_data($snippetrecordset): array {
160160
}
161161

162162
/**
163-
* Checks which snippets are active and returns their css.
163+
* Checks which snippets are active and returns their scss.
164164
*
165165
* @return string
166166
*/

tests/snippets_test.php

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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_enabled_snippet_scss
122+
*
123+
* @return void
124+
*/
125+
public function test_fetch_enabled_snippets_scss(): void {
126+
global $DB;
127+
128+
$this->resetAfterTest();
129+
130+
$scss = snippets::get_enabled_snippet_scss();
131+
132+
// No snippets are enabled by default, so the scss should be empty.
133+
$this->assertEquals('', $scss);
134+
135+
// Enable a snippet directly via the DB.
136+
$snippet = $DB->get_record(
137+
'theme_boost_union_snippets',
138+
['source' => 'theme_boost_union', 'path' => 'visual-depth.scss']
139+
);
140+
$snippet->enabled = 1;
141+
$DB->update_record('theme_boost_union_snippets', $snippet);
142+
143+
$scss = snippets::get_enabled_snippet_scss();
144+
145+
// Verify that the Snippets SCSS content is now queried.
146+
$this->assertNotEquals('', $scss);
147+
$this->assertStringContainsString('Snippet Title: Visual depth', $scss);
148+
}
149+
}

0 commit comments

Comments
 (0)