Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.52% covered (danger)
0.52%
1 / 191
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functions_content_get_context_test
0.52% covered (danger)
0.52%
1 / 191
33.33% covered (danger)
33.33%
1 / 3
11.86
0.00% covered (danger)
0.00%
0 / 1
 data_get_context
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 1
2
 data_get_context_unicode
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_context
100.00% covered (success)
100.00%
1 / 1
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
14use PHPUnit\Framework\TestCase;
15
16class phpbb_functions_content_get_context_test extends TestCase
17{
18    /**
19     * Data provider for get_context test cases.
20     *
21     * @return array
22     */
23    public static function data_get_context(): array
24    {
25        return [
26            'text contains words and length greater than text' => [
27                'text' => 'This is a sample text containing several words, including sample, text, and words.',
28                'words' => ['sample', 'words'],
29                'length' => 100,
30                'expected' => 'This is a sample text containing several words, including sample, text, and words.',
31            ],
32            'text contains words and length less than text' => [
33                'text' => 'This is a sample text containing several words, including sample, text, and words.',
34                'words' => ['sample', 'words'],
35                'length' => 50,
36                'expected' => 'This is a sample text containing several words ...',
37            ],
38            'text does not contain words' => [
39                'text' => 'This is a sample text containing several words, but none of them match the given words.',
40                'words' => ['nonexistent'],
41                'length' => 50,
42                'expected' => 'This is a sample text containing several words ...',
43            ],
44            'desired length equal to text length' => [
45                'text' => 'Exact length text.',
46                'words' => ['Exact', 'text'],
47                'length' => 18,
48                'expected' => 'Exact length text.',
49            ],
50            'text with html entities' => [
51                'text' => 'This is a sample text containing &amp; and &lt; and &gt; entities.',
52                'words' => ['sample', 'containing'],
53                'length' => 50,
54                'expected' => 'This is a sample text containing &amp; and &lt; and ...',
55            ],
56            'text with html entities and contains last word' => [
57                'text' => 'This is a sample text containing &amp; and &lt; and &gt; entities.',
58                'words' => ['sample', 'entities'],
59                'length' => 50,
60                'expected' => 'This is a sample text ... and &lt; and &gt; entities.',
61            ],
62            'text with multiple spaces and special characters' => [
63                'text' => 'This    is    a   sample   text containing    several   words.',
64                'words' => ['sample', 'several'],
65                'length' => 50,
66                'expected' => 'This is a sample text containing several words.',
67            ],
68            'empty text' => [
69                'text' => '',
70                'words' => ['sample', 'words'],
71                'length' => 50,
72                'expected' => '',
73            ],
74            'empty words array' => [
75                'text' => 'This is a sample text containing several words.',
76                'words' => [],
77                'length' => 50,
78                'expected' => 'This is a sample text containing several words.',
79            ],
80            'zero length' => [
81                'text' => 'This is a sample text.',
82                'words' => ['sample'],
83                'length' => 0,
84                'expected' => 'This is a sample text.',
85            ],
86            'negative length' => [
87                'text' => 'This is a sample text.',
88                'words' => ['sample'],
89                'length' => -10,
90                'expected' => 'This is a sample text.',
91            ],
92            'ellipses_beginning' => [
93                'text' => 'foo foo foo foo foo foo foo foo bar',
94                'words' => ['bar'],
95                'length' => 10,
96                'expected' => '... foo foo bar',
97            ],
98            'ellipsis_end' => [
99                'text' => 'bar foo foo foo foo foo foo foo foo',
100                'words' => ['bar'],
101                'length' => 10,
102                'expected' => 'bar foo foo ...',
103            ],
104            'ellipsis_middle' => [
105                'text' => 'foo word1 foo foo foo foo foo foo foo foo foo word2 foo',
106                'words' => ['word1', 'word2'],
107                'length' => 10,
108                'expected' => '... word1 ... word2 ...',
109            ],
110            'ellipsis_middle2' => [
111                'text' => 'word1 foo foo foo foo foo foo foo foo foo word2',
112                'words' => ['word1', 'word2'],
113                'length' => 10,
114                'expected' => 'word1 ... word2',
115            ],
116        ];
117    }
118
119    /**
120     * Data provider for unicode get_context test cases.
121     *
122     * @return array
123     */
124    public static function data_get_context_unicode(): array
125    {
126        return [
127            'text contains words and length greater than text unicode' => [
128                'text' => 'Это пример текста, содержащего разнообразные слова, включая пример, текст и слова.',
129                'words' => ['пример', 'слова'],
130                'length' => 100,
131                'expected' => 'Это пример текста, содержащего разнообразные слова, включая пример, текст и слова.',
132            ],
133            'text contains words and length less than text unicode' => [
134                'text' => 'Это пример текста, содержащего разнообразные слова, включая шаблон, текст и слова.',
135                'words' => ['пример', 'слова'],
136                'length' => 50,
137                'expected' => 'Это пример текста, содержащего разнообразные слова ...',
138            ],
139            'text does not contain words unicode' => [
140                'text' => 'Это пример текста, содержащего разнообразные слова, но ни одно из них не совпадает с искомыми.',
141                'words' => ['nonexistent'],
142                'length' => 50,
143                'expected' => 'Это пример текста, содержащего разнообразные слова ...',
144            ],
145            'desired length equal to text length unicode' => [
146                'text' => 'Текст точной длины.',
147                'words' => ['Текст', 'точной'],
148                'length' => 19,
149                'expected' => 'Текст точной длины.',
150            ],
151            'text with html entities unicode' => [
152                'text' => 'Это пример текста, содержащего &amp; и &lt; и &gt; лексемы.',
153                'words' => ['пример', 'содержащего'],
154                'length' => 40,
155                'expected' => 'Это пример текста, содержащего &amp; и &lt; и ...',
156            ],
157            'text with html entities and contains last word unicode' => [
158                'text' => 'Это пример текста, содержащего &amp; и &lt; и &gt; лексемы.',
159                'words' => ['пример', 'лексемы'],
160                'length' => 40,
161                'expected' => 'Это пример текста ... и &lt; и &gt; лексемы.',
162            ],
163            'text with multiple spaces and special characters unicode' => [
164                'text' => 'Это    пример   текста, содержащего    разнообразные   слова.',
165                'words' => ['пример', 'разнообразные'],
166                'length' => 50,
167                'expected' => 'Это пример текста, содержащего разнообразные слова.',
168            ],
169            'empty text unicode' => [
170                'text' => '',
171                'words' => ['пример', 'слова'],
172                'length' => 50,
173                'expected' => '',
174            ],
175            'empty words array unicode' => [
176                'text' => 'Это пример текста, содержащего разнообразные слова.',
177                'words' => [],
178                'length' => 50,
179                'expected' => 'Это пример текста, содержащего разнообразные слова.',
180            ],
181            'zero length unicode' => [
182                'text' => 'Это пример текста.',
183                'words' => ['пример'],
184                'length' => 0,
185                'expected' => 'Это пример текста.',
186            ],
187            'negative length unicode' => [
188                'text' => 'Это пример текста.',
189                'words' => ['sample'],
190                'length' => -10,
191                'expected' => 'Это пример текста.',
192            ],
193            'ellipses_beginning unicode' => [
194                'text' => 'раз раз раз раз раз раз раз раз два',
195                'words' => ['два'],
196                'length' => 10,
197                'expected' => '... раз раз два',
198            ],
199            'ellipsis_end unicode' => [
200                'text' => 'два раз раз раз раз раз раз раз раз',
201                'words' => ['два'],
202                'length' => 10,
203                'expected' => 'два раз раз ...',
204            ],
205            'ellipsis_middle unicode' => [
206                'text' => 'раз слово1 раз раз раз раз раз раз раз раз раз слово2 раз',
207                'words' => ['слово1', 'слово2'],
208                'length' => 15,
209                'expected' => '... слово1 ... слово2 ...',
210            ],
211            'ellipsis_middle2 unicode' => [
212                'text' => 'слово1 foo foo foo foo foo foo foo foo foo слово2',
213                'words' => ['слово1', 'слово2'],
214                'length' => 10,
215                'expected' => 'слово1 ... слово2',
216            ],
217            'fruits_spanish unicode' => [
218                'text' => 'Manzana,plátano,naranja,fresa,mango,uva,piña,pera,kiwi,cereza,sandía,melón,papaya,arándano,durazno',
219                'words' => ['piña'],
220                'length' => 20,
221                'expected' => '... uva,piña,pera ...',
222            ]
223        ];
224    }
225
226    /**
227     * @dataProvider data_get_context
228     * @dataProvider data_get_context_unicode
229     */
230    public function test_get_context($text, $words, $length, $expected)
231    {
232        $this->assertEquals($expected, get_context($text, $words, $length));
233    }
234
235}