Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.05% |
77 / 95 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_help_manager_test | |
81.05% |
77 / 95 |
|
66.67% |
4 / 6 |
7.33 | |
0.00% |
0 / 1 |
| setUp | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| add_block_data | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| test_add_block | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
2 | |||
| add_question_data | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| test_add_question | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| test_add_block_double_switch | |
100.00% |
27 / 27 |
|
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 | |
| 14 | class phpbb_help_manager_test extends phpbb_test_case |
| 15 | { |
| 16 | /** @var \phpbb\help\manager */ |
| 17 | protected $manager; |
| 18 | /** @var \phpbb\template\template */ |
| 19 | protected $template; |
| 20 | /** @var \phpbb\language\language */ |
| 21 | protected $language; |
| 22 | |
| 23 | protected function setUp(): void |
| 24 | { |
| 25 | $this->template = $this->getMockBuilder('\phpbb\template\template') |
| 26 | ->disableOriginalConstructor() |
| 27 | ->getMock(); |
| 28 | $this->language = $this->getMockBuilder('\phpbb\language\language') |
| 29 | ->disableOriginalConstructor() |
| 30 | ->getMock(); |
| 31 | |
| 32 | $this->manager = new \phpbb\help\manager( |
| 33 | new \phpbb_mock_event_dispatcher(), |
| 34 | $this->language, |
| 35 | $this->template |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | public static function add_block_data() |
| 40 | { |
| 41 | return array( |
| 42 | array('abc', false, array(), false), |
| 43 | array('def', true, array(), true), |
| 44 | array( |
| 45 | 'abc', |
| 46 | false, |
| 47 | array( |
| 48 | 'question1' => 'answer1', |
| 49 | 'question2' => 'answer2', |
| 50 | 'question3' => 'answer3', |
| 51 | ), |
| 52 | false |
| 53 | ), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @dataProvider add_block_data |
| 59 | * |
| 60 | * @param string $block_name |
| 61 | * @param bool $switch |
| 62 | * @param array $questions |
| 63 | * @param bool $switch_expected |
| 64 | */ |
| 65 | public function test_add_block($block_name, $switch, $questions, $switch_expected) |
| 66 | { |
| 67 | $question_ary = $question_ary_upper = $template_call_args = []; |
| 68 | foreach ($questions as $question => $answer) |
| 69 | { |
| 70 | $question_ary = array_merge($question_ary, [[$question], [$answer]]); |
| 71 | $question_ary_upper = array_merge($question_ary_upper, [strtoupper($question), strtoupper($answer)]); |
| 72 | $template_call_args = array_merge($template_call_args, [['faq_block.faq_row', [ |
| 73 | 'FAQ_QUESTION' => strtoupper($question), |
| 74 | 'FAQ_ANSWER' => strtoupper($answer), |
| 75 | ] |
| 76 | ]]); |
| 77 | } |
| 78 | |
| 79 | $this->language->expects($this->exactly(count($questions)*2 + 1)) |
| 80 | ->method('lang') |
| 81 | ->willReturnCallback(fn(string $arg) => strtoupper($arg)); |
| 82 | |
| 83 | $expected_calls = [ |
| 84 | ['faq_block', [ |
| 85 | 'BLOCK_TITLE' => strtoupper($block_name), |
| 86 | 'SWITCH_COLUMN' => $switch_expected, |
| 87 | ]], |
| 88 | ...$template_call_args |
| 89 | ]; |
| 90 | |
| 91 | $this->template->expects($this->exactly(count($questions) + 1)) |
| 92 | ->method('assign_block_vars') |
| 93 | ->willReturnCallback(function($blockName, $blockVars) use (&$expected_calls) { |
| 94 | static $call = 0; |
| 95 | $this->assertEquals($expected_calls[$call][0], $blockName); |
| 96 | $this->assertEquals($expected_calls[$call][1], $blockVars); |
| 97 | $call++; |
| 98 | }); |
| 99 | |
| 100 | $this->manager->add_block($block_name, $switch, $questions); |
| 101 | |
| 102 | $this->assertEquals($switch_expected, $this->manager->switched_column()); |
| 103 | } |
| 104 | |
| 105 | public static function add_question_data() |
| 106 | { |
| 107 | return array( |
| 108 | array('question1', 'answer1'), |
| 109 | array('question2', 'answer2'), |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @dataProvider add_question_data |
| 115 | * |
| 116 | * @param string $question |
| 117 | * @param string $answer |
| 118 | */ |
| 119 | public function test_add_question($question, $answer) |
| 120 | { |
| 121 | $this->language->expects($this->exactly(2)) |
| 122 | ->method('lang') |
| 123 | ->willReturnCallback(fn(string $arg) => strtoupper($arg)); |
| 124 | |
| 125 | $this->template->expects($this->once()) |
| 126 | ->method('assign_block_vars') |
| 127 | ->with('faq_block.faq_row', array( |
| 128 | 'FAQ_QUESTION' => strtoupper($question), |
| 129 | 'FAQ_ANSWER' => strtoupper($answer), |
| 130 | )); |
| 131 | |
| 132 | $this->manager->add_question($question, $answer); |
| 133 | } |
| 134 | |
| 135 | public function test_add_block_double_switch() |
| 136 | { |
| 137 | $block_name = ['abc', 'def']; |
| 138 | $switch_expected = [true, false]; |
| 139 | |
| 140 | $this->language->expects($this->exactly(2)) |
| 141 | ->method('lang') |
| 142 | ->willReturnCallback(fn(string $arg) => strtoupper($arg)); |
| 143 | |
| 144 | $expected_calls = [ |
| 145 | ['faq_block', [ |
| 146 | 'BLOCK_TITLE' => strtoupper($block_name[0]), |
| 147 | 'SWITCH_COLUMN' => $switch_expected[0], |
| 148 | ]], |
| 149 | ['faq_block', [ |
| 150 | 'BLOCK_TITLE' => strtoupper($block_name[1]), |
| 151 | 'SWITCH_COLUMN' => $switch_expected[1], |
| 152 | ]] |
| 153 | ]; |
| 154 | |
| 155 | $this->template->expects($this->exactly(count($expected_calls))) |
| 156 | ->method('assign_block_vars') |
| 157 | ->willReturnCallback(function($blockName, $blockVars) use (&$expected_calls) { |
| 158 | static $call = 0; |
| 159 | $this->assertEquals($expected_calls[$call][0], $blockName); |
| 160 | $this->assertEquals($expected_calls[$call][1], $blockVars); |
| 161 | $call++; |
| 162 | }); |
| 163 | |
| 164 | $this->manager->add_block($block_name[0], true); |
| 165 | $this->assertTrue($this->manager->switched_column()); |
| 166 | |
| 167 | // Add a second block with switch |
| 168 | $this->manager->add_block($block_name[1], true); |
| 169 | $this->assertTrue($this->manager->switched_column()); |
| 170 | } |
| 171 | } |