| 1 | efrain | 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 core;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core_renderer;
 | 
        
           |  |  | 20 | use moodle_page;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | /**
 | 
        
           |  |  | 23 |  * Tests for \core_renderer.
 | 
        
           |  |  | 24 |  *
 | 
        
           |  |  | 25 |  * @package   core
 | 
        
           |  |  | 26 |  * @category  test
 | 
        
           |  |  | 27 |  * @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
 | 
        
           |  |  | 28 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 29 |  * @covers \core_renderer
 | 
        
           |  |  | 30 |  */
 | 
        
           |  |  | 31 | final class core_renderer_test extends \advanced_testcase {
 | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      * @covers \core\hook\before_standard_top_of_body_html_generation
 | 
        
           |  |  | 34 |      */
 | 
        
           |  |  | 35 |     public function test_standard_top_of_body_html(): void {
 | 
        
           |  |  | 36 |         $page = new moodle_page();
 | 
        
           |  |  | 37 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |         $html = $renderer->standard_top_of_body_html();
 | 
        
           |  |  | 40 |         $this->assertIsString($html);
 | 
        
           |  |  | 41 |         $this->assertStringNotContainsString('A heading can be added to the top of the body HTML', $html);
 | 
        
           |  |  | 42 |     }
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      * @covers \core\hook\before_standard_top_of_body_html_generation
 | 
        
           |  |  | 46 |      */
 | 
        
           |  |  | 47 |     public function test_before_standard_top_of_body_html_generation_hooked(): void {
 | 
        
           |  |  | 48 |         require_once(__DIR__ . '/fixtures/core_renderer/before_standard_top_of_body_html_generation_callbacks.php');
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         \core\di::set(
 | 
        
           |  |  | 51 |             \core\hook\manager::class,
 | 
        
           |  |  | 52 |             \core\hook\manager::phpunit_get_instance([
 | 
        
           |  |  | 53 |                 'test_plugin1' => __DIR__ . '/fixtures/core_renderer/before_standard_top_of_body_html_generation_hooks.php',
 | 
        
           |  |  | 54 |             ]),
 | 
        
           |  |  | 55 |         );
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |         $page = new moodle_page();
 | 
        
           |  |  | 58 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         $html = $renderer->standard_top_of_body_html();
 | 
        
           |  |  | 61 |         $this->assertIsString($html);
 | 
        
           |  |  | 62 |         $this->assertStringContainsString('A heading can be added to the top of the body HTML', $html);
 | 
        
           |  |  | 63 |     }
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |     /**
 | 
        
           |  |  | 66 |      * @covers \core\hook\before_footer_html_generation
 | 
        
           |  |  | 67 |      */
 | 
        
           |  |  | 68 |     public function test_before_footer_html_generation(): void {
 | 
        
           |  |  | 69 |         $this->resetAfterTest();
 | 
        
           |  |  | 70 |         $page = new moodle_page();
 | 
        
           |  |  | 71 |         $page->set_state(moodle_page::STATE_PRINTING_HEADER);
 | 
        
           |  |  | 72 |         $page->set_state(moodle_page::STATE_IN_BODY);
 | 
        
           |  |  | 73 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |         $page->opencontainers->push('header/footer', '</body></html>');
 | 
        
           |  |  | 76 |         $html = $renderer->footer();
 | 
        
           |  |  | 77 |         $this->assertIsString($html);
 | 
        
           |  |  | 78 |         $this->assertStringNotContainsString('A heading can be added', $html);
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * @covers \core\hook\before_footer_html_generation
 | 
        
           |  |  | 83 |      */
 | 
        
           |  |  | 84 |     public function test_before_footer_html_generation_hooked(): void {
 | 
        
           |  |  | 85 |         $this->resetAfterTest();
 | 
        
           |  |  | 86 |         require_once(__DIR__ . '/fixtures/core_renderer/before_footer_html_generation_callbacks.php');
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |         \core\di::set(
 | 
        
           |  |  | 89 |             \core\hook\manager::class,
 | 
        
           |  |  | 90 |             \core\hook\manager::phpunit_get_instance([
 | 
        
           |  |  | 91 |                 'test_plugin1' => __DIR__ . '/fixtures/core_renderer/before_footer_html_generation_hooks.php',
 | 
        
           |  |  | 92 |             ]),
 | 
        
           |  |  | 93 |         );
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         $page = new moodle_page();
 | 
        
           |  |  | 96 |         $page->set_state(moodle_page::STATE_PRINTING_HEADER);
 | 
        
           |  |  | 97 |         $page->set_state(moodle_page::STATE_IN_BODY);
 | 
        
           |  |  | 98 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         $page->opencontainers->push('header/footer', '</body></html>');
 | 
        
           |  |  | 101 |         $html = $renderer->footer();
 | 
        
           |  |  | 102 |         $this->assertIsString($html);
 | 
        
           |  |  | 103 |         $this->assertStringContainsString('A heading can be added', $html);
 | 
        
           |  |  | 104 |     }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     /**
 | 
        
           |  |  | 107 |      * @covers \core\hook\before_standard_footer_html_generation
 | 
        
           |  |  | 108 |      */
 | 
        
           |  |  | 109 |     public function before_standard_footer_html_generation(): void {
 | 
        
           |  |  | 110 |         $page = new moodle_page();
 | 
        
           |  |  | 111 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |         $html = $renderer->standard_footer_html();
 | 
        
           |  |  | 114 |         $this->assertIsString($html);
 | 
        
           |  |  | 115 |         $this->assertStringNotContainsString('A heading can be added', $html);
 | 
        
           |  |  | 116 |     }
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 |     /**
 | 
        
           |  |  | 119 |      * @covers \core\hook\before_standard_footer_html_generation
 | 
        
           |  |  | 120 |      */
 | 
        
           |  |  | 121 |     public function test_before_standard_footer_html_generation_hooked(): void {
 | 
        
           |  |  | 122 |         require_once(__DIR__ . '/fixtures/core_renderer/before_standard_footer_html_generation_callbacks.php');
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |         \core\di::set(
 | 
        
           |  |  | 125 |             \core\hook\manager::class,
 | 
        
           |  |  | 126 |             \core\hook\manager::phpunit_get_instance([
 | 
        
           |  |  | 127 |                 'test_plugin1' => __DIR__ . '/fixtures/core_renderer/before_standard_footer_html_generation_hooks.php',
 | 
        
           |  |  | 128 |             ]),
 | 
        
           |  |  | 129 |         );
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |         $page = new moodle_page();
 | 
        
           |  |  | 132 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |         $html = $renderer->standard_footer_html();
 | 
        
           |  |  | 135 |         $this->assertIsString($html);
 | 
        
           |  |  | 136 |         $this->assertStringContainsString('A heading can be added', $html);
 | 
        
           |  |  | 137 |     }
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |     /**
 | 
        
           |  |  | 140 |      * @covers \core\hook\after_standard_main_region_html_generation
 | 
        
           |  |  | 141 |      */
 | 
        
           |  |  | 142 |     public function test_after_standard_main_region_html_generation(): void {
 | 
        
           |  |  | 143 |         $page = new moodle_page();
 | 
        
           |  |  | 144 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 145 |   | 
        
           |  |  | 146 |         $html = $renderer->standard_after_main_region_html();
 | 
        
           |  |  | 147 |         $this->assertIsString($html);
 | 
        
           |  |  | 148 |         $this->assertStringNotContainsString('A heading can be added', $html);
 | 
        
           |  |  | 149 |     }
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |     /**
 | 
        
           |  |  | 152 |      * @covers \core\hook\after_standard_main_region_html_generation
 | 
        
           |  |  | 153 |      */
 | 
        
           |  |  | 154 |     public function test_after_standard_main_region_html_generation_hooked(): void {
 | 
        
           |  |  | 155 |         require_once(__DIR__ . '/fixtures/core_renderer/after_standard_main_region_html_generation_callbacks.php');
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 |         \core\di::set(
 | 
        
           |  |  | 158 |             \core\hook\manager::class,
 | 
        
           |  |  | 159 |             \core\hook\manager::phpunit_get_instance([
 | 
        
           |  |  | 160 |                 'test_plugin1' => __DIR__ . '/fixtures/core_renderer/after_standard_main_region_html_generation_hooks.php',
 | 
        
           |  |  | 161 |             ]),
 | 
        
           |  |  | 162 |         );
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 |         $page = new moodle_page();
 | 
        
           |  |  | 165 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |         $html = $renderer->standard_after_main_region_html();
 | 
        
           |  |  | 168 |         $this->assertIsString($html);
 | 
        
           |  |  | 169 |         $this->assertStringContainsString('A heading can be added', $html);
 | 
        
           |  |  | 170 |     }
 | 
        
           |  |  | 171 |   | 
        
           |  |  | 172 |     /**
 | 
        
           |  |  | 173 |      * @covers \core\hook\before_html_attributes
 | 
        
           |  |  | 174 |      */
 | 
        
           |  |  | 175 |     public function test_htmlattributes(): void {
 | 
        
           |  |  | 176 |         $page = new moodle_page();
 | 
        
           |  |  | 177 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |         $attributes = $renderer->htmlattributes();
 | 
        
           |  |  | 180 |         $this->assertIsString($attributes);
 | 
        
           |  |  | 181 |         $this->assertStringNotContainsString('data-test="test"', $attributes);
 | 
        
           |  |  | 182 |     }
 | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 |     /**
 | 
        
           |  |  | 185 |      * @covers \core\hook\before_html_attributes
 | 
        
           |  |  | 186 |      */
 | 
        
           |  |  | 187 |     public function test_htmlattributes_hooked(): void {
 | 
        
           |  |  | 188 |         require_once(__DIR__ . '/fixtures/core_renderer/htmlattributes_callbacks.php');
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |         \core\di::set(
 | 
        
           |  |  | 191 |             \core\hook\manager::class,
 | 
        
           |  |  | 192 |             \core\hook\manager::phpunit_get_instance([
 | 
        
           |  |  | 193 |                 'test_plugin1' => __DIR__ . '/fixtures/core_renderer/htmlattributes_hooks.php',
 | 
        
           |  |  | 194 |             ]),
 | 
        
           |  |  | 195 |         );
 | 
        
           |  |  | 196 |   | 
        
           |  |  | 197 |         $page = new moodle_page();
 | 
        
           |  |  | 198 |         $renderer = new core_renderer($page, RENDERER_TARGET_GENERAL);
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 |         $attributes = $renderer->htmlattributes();
 | 
        
           |  |  | 201 |         $this->assertIsString($attributes);
 | 
        
           |  |  | 202 |         $this->assertStringContainsString('data-test="test"', $attributes);
 | 
        
           |  |  | 203 |     }
 | 
        
           |  |  | 204 | }
 |