|
| 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 format_onetopic; |
| 18 | + |
| 19 | +use format_onetopic\local\hooks\output\before_http_headers; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests for styles.php caching functionality. |
| 23 | + * |
| 24 | + * @package format_onetopic |
| 25 | + * @author Jonathan Archer <jonathanarcher@catalyst-au.net> |
| 26 | + * @copyright Catalyst IT |
| 27 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 28 | + * @covers \format_onetopic\local\hooks\output\before_http_headers::get_tabstyles_revision |
| 29 | + */ |
| 30 | +class styles_caching_test extends \advanced_testcase { |
| 31 | + |
| 32 | + /** |
| 33 | + * Test revision is '0' when no tab styles configured. |
| 34 | + */ |
| 35 | + public function test_revision_empty_when_no_styles(): void { |
| 36 | + $this->resetAfterTest(true); |
| 37 | + |
| 38 | + set_config('tabstyles', '', 'format_onetopic'); |
| 39 | + |
| 40 | + $revision = before_http_headers::get_tabstyles_revision(); |
| 41 | + $this->assertEquals('0', $revision); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test revision is valid hash when tab styles exist. |
| 46 | + */ |
| 47 | + public function test_revision_is_hash_when_styles_exist(): void { |
| 48 | + $this->resetAfterTest(true); |
| 49 | + |
| 50 | + $tabstyles = json_encode(['default' => ['color' => 'red']]); |
| 51 | + set_config('tabstyles', $tabstyles, 'format_onetopic'); |
| 52 | + |
| 53 | + $revision = before_http_headers::get_tabstyles_revision(); |
| 54 | + |
| 55 | + $this->assertIsString($revision); |
| 56 | + $this->assertEquals(8, strlen($revision)); |
| 57 | + $this->assertMatchesRegularExpression('/^[a-f0-9]{8}$/', $revision); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test revision changes when tab styles change. |
| 62 | + */ |
| 63 | + public function test_revision_changes_when_styles_change(): void { |
| 64 | + $this->resetAfterTest(true); |
| 65 | + |
| 66 | + $tabstyles1 = json_encode(['default' => ['color' => 'red']]); |
| 67 | + set_config('tabstyles', $tabstyles1, 'format_onetopic'); |
| 68 | + $revision1 = before_http_headers::get_tabstyles_revision(); |
| 69 | + |
| 70 | + $tabstyles2 = json_encode(['default' => ['color' => 'blue']]); |
| 71 | + set_config('tabstyles', $tabstyles2, 'format_onetopic'); |
| 72 | + $revision2 = before_http_headers::get_tabstyles_revision(); |
| 73 | + |
| 74 | + $this->assertNotEquals($revision1, $revision2); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test revision stays same for identical styles. |
| 79 | + */ |
| 80 | + public function test_revision_consistent_for_same_styles(): void { |
| 81 | + $this->resetAfterTest(true); |
| 82 | + |
| 83 | + $tabstyles = json_encode(['default' => ['color' => 'red']]); |
| 84 | + set_config('tabstyles', $tabstyles, 'format_onetopic'); |
| 85 | + |
| 86 | + $revision1 = before_http_headers::get_tabstyles_revision(); |
| 87 | + $revision2 = before_http_headers::get_tabstyles_revision(); |
| 88 | + |
| 89 | + $this->assertEquals($revision1, $revision2); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test revision is deterministic (same input = same hash). |
| 94 | + */ |
| 95 | + public function test_revision_is_deterministic(): void { |
| 96 | + $this->resetAfterTest(true); |
| 97 | + |
| 98 | + $tabstyles = json_encode(['default' => ['color' => 'red']]); |
| 99 | + set_config('tabstyles', $tabstyles, 'format_onetopic'); |
| 100 | + |
| 101 | + $expected = substr(md5($tabstyles), 0, 8); |
| 102 | + $actual = before_http_headers::get_tabstyles_revision(); |
| 103 | + |
| 104 | + $this->assertEquals($expected, $actual); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments