Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
72 / 75
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_template_template_test_case
96.00% covered (success)
96.00%
72 / 75
85.71% covered (warning)
85.71%
6 / 7
14
0.00% covered (danger)
0.00%
0 / 1
 setUpBeforeClass
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 display
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 trim_template_result
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 config_defaults
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setup_engine
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
2
 setUp
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 run_template
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3use phpbb\template\twig\twig;
4
5/**
6*
7* This file is part of the phpBB Forum Software package.
8*
9* @copyright (c) phpBB Limited <https://www.phpbb.com>
10* @license GNU General Public License, version 2 (GPL-2.0)
11*
12* For full copyright and license information, please see
13* the docs/CREDITS.txt file.
14*
15*/
16
17class phpbb_template_template_test_case extends phpbb_test_case
18{
19    protected $lang;
20    /** @var twig */
21    protected $template;
22    protected $template_path;
23    protected $user;
24
25    protected $test_path = 'tests/template';
26
27    // Keep the contents of the cache for debugging?
28    const PRESERVE_CACHE = true;
29
30    protected static $language_reflection_lang;
31
32    public static function setUpBeforeClass(): void
33    {
34        parent::setUpBeforeClass();
35
36        $reflection = new ReflectionClass('\phpbb\language\language');
37        self::$language_reflection_lang = $reflection->getProperty('lang');
38    }
39
40    protected function display($handle)
41    {
42        ob_start();
43
44        try
45        {
46            $this->template->display($handle, false);
47        }
48        catch (Exception $exception)
49        {
50            // reset output buffering even when an error occurred
51            // PHPUnit turns trigger_error into exceptions as well
52            ob_end_clean();
53            throw $exception;
54        }
55
56        $result = self::trim_template_result(ob_get_clean());
57
58        return $result;
59    }
60
61    protected static function trim_template_result($result)
62    {
63        return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result)))));
64    }
65
66    protected function config_defaults()
67    {
68        $defaults = array(
69            'load_tplcompile'    => true,
70        );
71        return $defaults;
72    }
73
74    protected function setup_engine(array $new_config = array(), string $template_path = '')
75    {
76        global $phpbb_root_path, $phpEx;
77
78        $defaults = $this->config_defaults();
79        $config = new \phpbb\config\config(array_merge($defaults, $new_config));
80        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
81        $this->lang = $lang = new \phpbb\language\language($lang_loader);
82        $user = new \phpbb\user($lang, '\phpbb\datetime');
83        $this->user = $user;
84
85        $filesystem = new \phpbb\filesystem\filesystem();
86
87        $path_helper = new \phpbb\path_helper(
88            new \phpbb\symfony_request(
89                new phpbb_mock_request()
90            ),
91            $this->createMock('\phpbb\request\request'),
92            $phpbb_root_path,
93            $phpEx
94        );
95
96        $this->template_path = $template_path ?: $this->test_path . '/templates';
97
98        $cache_path = $phpbb_root_path . 'cache/twig';
99        $context = new \phpbb\template\context();
100        $loader = new \phpbb\template\twig\loader('');
101        $log = new \phpbb\log\dummy();
102        $assets_bag = new \phpbb\template\assets_bag();
103        $twig = new \phpbb\template\twig\environment(
104            $assets_bag,
105            $config,
106            $filesystem,
107            $path_helper,
108            $cache_path,
109            null,
110            $loader,
111            new \phpbb\event\dispatcher(),
112            array(
113                'cache'            => false,
114                'debug'            => false,
115                'auto_reload'    => true,
116                'autoescape'    => false,
117            )
118        );
119        $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $lang)));
120        $twig->setLexer(new \phpbb\template\twig\lexer($twig));
121        $this->template->set_custom_style('tests', $this->template_path);
122    }
123
124    protected function setUp(): void
125    {
126        // Test the engine can be used
127        $this->setup_engine();
128
129        global $phpbb_filesystem;
130
131        $phpbb_filesystem = new \phpbb\filesystem\filesystem();
132    }
133
134    protected function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $lang_vars = array())
135    {
136        $this->template->set_filenames(array('test' => $file));
137        $this->template->assign_vars($vars);
138
139        foreach ($block_vars as $block => $loops)
140        {
141            foreach ($loops as $_vars)
142            {
143                $this->template->assign_block_vars($block, $_vars);
144            }
145        }
146
147        foreach ($destroy as $block)
148        {
149            $this->template->destroy_block_vars($block);
150        }
151
152        // Previous functionality was $cachefile (string), which was removed, check to prevent errors
153        if (is_array($lang_vars))
154        {
155            foreach ($lang_vars as $name => $value)
156            {
157                self::$language_reflection_lang->setValue($this->lang, array_merge(
158                    self::$language_reflection_lang->getValue($this->lang),
159                    array($name => $value)
160                ));
161            }
162        }
163
164        $expected = str_replace(array("\n", "\r", "\t"), '', $expected);
165        $output = str_replace(array("\n", "\r", "\t"), '', $this->display('test'));
166
167        $this->assertEquals($expected, $output, "Testing $file");
168    }
169}