Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.54% covered (success)
92.54%
62 / 67
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
twig_test
92.54% covered (success)
92.54%
62 / 67
60.00% covered (warning)
60.00%
3 / 5
5.01
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
1
 test_get_user_style_invalid_user
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
1.00
 data_get_user_style
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_user_style
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_set_style
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\tests\template;
15
16use phpbb\filesystem\helper as filesystem_helper;
17use phpbb\template\twig\twig;
18
19class twig_test extends \phpbb_test_case
20{
21    /** @var twig */
22    public $twig;
23    /**
24     * @var string
25     */
26    private $template_path;
27    /**
28     * @var twig
29     */
30    private $template;
31    /**
32     * @var \phpbb\user
33     */
34    private $user;
35    /**
36     * @var \phpbb\language\language
37     */
38    private $lang;
39
40    protected function setUp(): void
41    {
42        global $phpbb_root_path, $phpEx;
43
44        $config = new \phpbb\config\config([]);
45        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
46        $this->lang = $lang = new \phpbb\language\language($lang_loader);
47        $user = new \phpbb\user($lang, '\phpbb\datetime');
48        $this->user = $user;
49
50        $filesystem = new \phpbb\filesystem\filesystem();
51
52        $path_helper = new \phpbb\path_helper(
53            new \phpbb\symfony_request(
54                new \phpbb_mock_request()
55            ),
56            $this->createMock('\phpbb\request\request'),
57            $phpbb_root_path,
58            $phpEx
59        );
60
61        $this->template_path = 'tests/template/templates';
62
63        $cache_path = $phpbb_root_path . 'cache/twig';
64        $context = new \phpbb\template\context();
65        $loader = new \phpbb\template\twig\loader('');
66        $log = new \phpbb\log\dummy();
67        $assets_bag = new \phpbb\template\assets_bag();
68        $twig = new \phpbb\template\twig\environment(
69            $assets_bag,
70            $config,
71            $filesystem,
72            $path_helper,
73            $cache_path,
74            null,
75            $loader,
76            new \phpbb\event\dispatcher(),
77            [
78                'cache'            => false,
79                'debug'            => false,
80                'auto_reload'    => true,
81                'autoescape'    => false,
82            ]
83        );
84        $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
85        $twig->setLexer(new \phpbb\template\twig\lexer($twig));
86    }
87
88    public function test_get_user_style_invalid_user()
89    {
90        // Add closure to override user method
91        $set_user_closure = function ($user) {
92            $this->user = $user;
93        };
94
95        $run_set_user_closure = $set_user_closure->bindTo($this->template, get_class($this->template));
96        $run_set_user_closure(null);
97
98        $this->expectException('\phpbb\template\exception\user_object_not_available');
99        $this->template->get_user_style();
100        $run_set_user_closure($this->user);
101    }
102
103    public static function data_get_user_style(): array
104    {
105        return [
106            [['style_path' => 'prosilver', 'style_parent_id' => 0], ['prosilver']],
107            [['style_path' => 'prosilver_se', 'style_parent_id' => 5, 'style_parent_tree' => 'prosilver'], ['prosilver_se', 'prosilver']],
108        ];
109    }
110
111    /**
112     * @dataProvider data_get_user_style
113     */
114    public function test_get_user_style($user_style, $expected)
115    {
116        $this->user->style = $user_style;
117        $this->assertEquals($expected, $this->template->get_user_style());
118    }
119
120    public function test_set_style()
121    {
122        global $phpbb_root_path;
123
124        // User style is left empty on purpose to see template as valid directory
125        $tests_template_relative_path = '../tests/template';
126        $test_template_absolute_path = filesystem_helper::realpath($phpbb_root_path . trim($tests_template_relative_path, '/'));
127
128        // Get loader instance
129        $template_reflection = new \ReflectionObject($this->template);
130        $loader_reflection = $template_reflection->getProperty('loader');
131        /** @var \phpbb\template\twig\loader $loader */
132        $loader = $loader_reflection->getValue($this->template);
133
134        // set_style() not called yet
135        $this->assertEmpty($loader->getSafeDirectories());
136
137        // set_style() to add default elements
138        $this->user->style = ['style_path' => '', 'style_parent_id' => 0];
139        $this->template->set_style();
140        $safe_directories = $loader->getSafeDirectories();
141        $this->assertFalse(in_array($test_template_absolute_path, $safe_directories));
142        $this->assertFalse(empty($safe_directories));
143
144        // set_style() with tests template folder
145        $this->template->set_style([$tests_template_relative_path]);
146        $this->assertTrue(in_array($test_template_absolute_path, $loader->getSafeDirectories()));
147    }
148}