Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 23... Línea 23...
23
 *
23
 *
24
 * @package   core
24
 * @package   core
25
 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
25
 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
27
 */
28
class mustache_template_source_loader_test extends \advanced_testcase {
28
final class mustache_template_source_loader_test extends \advanced_testcase {
29
    /**
29
    /**
30
     * Ensure that stripping comments from templates does not mutilate the template body.
30
     * Ensure that stripping comments from templates does not mutilate the template body.
31
     */
31
     */
32
    public function test_strip_template_comments(): void {
32
    public function test_strip_template_comments(): void {
Línea 132... Línea 132...
132
    }
132
    }
Línea 133... Línea 133...
133
 
133
 
134
    /**
134
    /**
135
     * Data provider for the test_load function.
135
     * Data provider for the test_load function.
136
     */
136
     */
137
    public function load_test_cases() {
137
    public static function load_test_cases(): array {
138
        $cache = [
138
        $cache = [
139
            'core' => [
139
            'core' => [
140
                'test' => '{{! a comment }}The rest of the template'
140
                'test' => '{{! a comment }}The rest of the template'
141
            ]
141
            ]
142
        ];
142
        ];
Línea 143... Línea 143...
143
        $loader = $this->build_loader_from_static_cache($cache);
143
        $loader = self::build_loader_from_static_cache($cache);
144
 
144
 
145
        return [
145
        return [
146
            'with comments' => [
146
            'with comments' => [
Línea 175... Línea 175...
175
    }
175
    }
Línea 176... Línea 176...
176
 
176
 
177
    /**
177
    /**
178
     * Data provider for the load_with_dependencies function.
178
     * Data provider for the load_with_dependencies function.
179
     */
179
     */
180
    public function load_with_dependencies_test_cases() {
180
    public static function load_with_dependencies_test_cases(): array {
181
        // Create a bunch of templates that include one another in various ways. There is
181
        // Create a bunch of templates that include one another in various ways. There is
182
        // multiple instances of recursive inclusions to test that the code doensn't get
182
        // multiple instances of recursive inclusions to test that the code doensn't get
183
        // stuck in an infinite loop.
183
        // stuck in an infinite loop.
184
        $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
184
        $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
Línea 203... Línea 203...
203
                'foo' => $foo2,
203
                'foo' => $foo2,
204
                'bop' => $bop,
204
                'bop' => $bop,
205
                'bim' => $bim
205
                'bim' => $bim
206
            ]
206
            ]
207
        ];
207
        ];
208
        $loader = $this->build_loader_from_static_cache($cache);
208
        $loader = self::build_loader_from_static_cache($cache);
Línea 209... Línea 209...
209
 
209
 
210
        return [
210
        return [
211
            'no template includes w comments' => [
211
            'no template includes w comments' => [
212
                'loader' => $loader,
212
                'loader' => $loader,
Línea 342... Línea 342...
342
        $this->assertEquals($expected, $actual);
342
        $this->assertEquals($expected, $actual);
343
    }
343
    }
344
    /**
344
    /**
345
     * Data provider for the test_load function.
345
     * Data provider for the test_load function.
346
     */
346
     */
347
    public function scan_template_source_for_dependencies_test_cases() {
347
    public static function scan_template_source_for_dependencies_test_cases(): array {
348
        $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
348
        $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
349
        $bar = '{{! a comment }}{{> core/baz }}';
349
        $bar = '{{! a comment }}{{> core/baz }}';
350
        $baz = '{{! a comment }}{{#str}} hide, core {{/str}}';
350
        $baz = '{{! a comment }}{{#str}} hide, core {{/str}}';
351
        $bop = '{{! a comment }}hello';
351
        $bop = '{{! a comment }}hello';
352
        $multiline1 = <<<TEMPLATE
352
        $multiline1 = <<<TEMPLATE
Línea 386... Línea 386...
386
                'multiline3' => $multiline3,
386
                'multiline3' => $multiline3,
387
                'multiline4' => $multiline4,
387
                'multiline4' => $multiline4,
388
                'multiline5' => $multiline5,
388
                'multiline5' => $multiline5,
389
            ]
389
            ]
390
        ];
390
        ];
391
        $loader = $this->build_loader_from_static_cache($cache);
391
        $loader = self::build_loader_from_static_cache($cache);
Línea 392... Línea 392...
392
 
392
 
393
        return [
393
        return [
394
            'single template include' => [
394
            'single template include' => [
395
                'loader' => $loader,
395
                'loader' => $loader,
Línea 486... Línea 486...
486
    }
486
    }
Línea 487... Línea 487...
487
 
487
 
488
    /**
488
    /**
489
     * Test the scan_template_source_for_dependencies function.
489
     * Test the scan_template_source_for_dependencies function.
490
     *
490
     *
491
     * @dataProvider scan_template_source_for_dependencies_test_cases()
491
     * @dataProvider scan_template_source_for_dependencies_test_cases
492
     * @param mustache_template_source_loader $loader The loader
492
     * @param mustache_template_source_loader $loader The loader
493
     * @param string $source The template to test
493
     * @param string $source The template to test
494
     * @param string $expected The expected output
494
     * @param string $expected The expected output
495
     */
495
     */
Línea 508... Línea 508...
508
     * from the given cache rather than disk.
508
     * from the given cache rather than disk.
509
     *
509
     *
510
     * @param array $cache A cache of templates
510
     * @param array $cache A cache of templates
511
     * @return mustache_template_source_loader
511
     * @return mustache_template_source_loader
512
     */
512
     */
513
    private function build_loader_from_static_cache(array $cache): mustache_template_source_loader {
513
    private static function build_loader_from_static_cache(array $cache): mustache_template_source_loader {
514
        return new mustache_template_source_loader(function($component, $name, $themename) use ($cache) {
514
        return new mustache_template_source_loader(function($component, $name, $themename) use ($cache) {
515
            return $cache[$component][$name];
515
            return $cache[$component][$name];
516
        });
516
        });
517
    }
517
    }
518
}
518
}