Passed
Push — master ( f02495...4136df )
by Alexander
01:44
created

TabsTest::testIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 75
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 45
nc 2
nop 0
dl 0
loc 75
rs 9.2
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4\Tests;
6
7
use Yiisoft\Yii\Bootstrap4\Html;
8
use Yiisoft\Yii\Bootstrap4\Tabs;
9
10
/**
11
 * Tests for Tabs widget.
12
 *
13
 * TabsTest
14
 */
15
final class TabsTest extends TestCase
16
{
17
    public function testRoleTabList(): void
18
    {
19
        Tabs::counter(0);
20
21
        $html = Tabs::widget()
22
            ->items([
23
                [
24
                    'label' => 'Page1',
25
                    'content' => 'Page1',
26
                ],
27
                [
28
                    'label' => 'Page2',
29
                    'content' => 'Page2',
30
                ],
31
            ])
32
            ->render();
33
34
        $this->assertStringContainsString('<ul id="w0-tabs" class="nav nav-tabs" role="tablist">', $html);
35
    }
36
37
    /**
38
     * Each tab should have a corresponding unique ID
39
     *
40
     * {@see https://github.com/yiisoft/yii2/issues/6150}
41
     */
42
    public function testIds(): void
43
    {
44
        Tabs::counter(0);
45
46
        $html = Tabs::widget()
47
            ->items([
48
                [
49
                    'label' => 'Page1',
50
                    'content' => 'Page1',
51
                ],
52
                [
53
                    'label' => 'Dropdown1',
54
                    'items' => [
55
                        ['label' => 'Page2', 'content' => 'Page2'],
56
                        ['label' => 'Page3', 'content' => 'Page3'],
57
                    ]
58
                ],
59
                [
60
                    'label' => 'Dropdown2',
61
                    'items' => [
62
                        ['label' => 'Page4', 'content' => 'Page4'],
63
                        ['label' => 'Page5', 'content' => 'Page5'],
64
                    ]
65
                ],
66
                [
67
                    'label' => $extAnchor1 = 'External link',
68
                    'url' => $extUrl1 = ['//other/route'],
69
                ],
70
                [
71
                    'label' => 'Dropdown3',
72
                    'items' => [
73
                        [
74
                            'label' => $extAnchor2 = 'External Dropdown Link',
75
                            'url' => $extUrl2 = ['//other/dropdown/route']
76
                        ],
77
                    ]
78
                ],
79
            ])
80
            ->render();
81
82
        $page1 = 'w0-tabs-tab0';
83
        $page2 = 'w0-tabs-dd1-tab0';
84
        $page3 = 'w0-tabs-dd1-tab1';
85
        $page4 = 'w0-tabs-dd2-tab0';
86
        $page5 = 'w0-tabs-dd2-tab1';
87
88
        $shouldContain = [
89
            'w0-tabs', // nav widget container
90
            "#$page1", // Page1
91
92
            'w1', // Dropdown1
93
            "$page2", // Page2
94
            "$page3", // Page3
95
96
97
            'w2', // Dropdown2
98
            "#$page4", // Page4
99
            "#$page5", // Page5
100
101
            'w3', // Dropdown3
102
103
            // containers
104
            "id=\"$page1\"",
105
            "id=\"$page2\"",
106
            "id=\"$page3\"",
107
            "id=\"$page4\"",
108
            "id=\"$page5\"",
109
            Html::a($extAnchor1, $extUrl1, ['class' => 'nav-link']),
110
            Html::a($extAnchor2, $extUrl2, [/*'tabindex' => -1, */
111
                'class' => 'dropdown-item'
112
            ]),
113
        ];
114
115
        foreach ($shouldContain as $string) {
116
            $this->assertStringContainsString($string, $html);
117
        }
118
    }
119
120
    public function testVisible(): void
121
    {
122
        Tabs::counter(0);
123
124
        $html = Tabs::widget()
125
            ->items([
126
                [
127
                    'label' => 'Page1',
128
                    'content' => 'Page1',
129
                ],
130
                [
131
                    'label' => 'InvisiblePage',
132
                    'content' => 'Invisible Page Content',
133
                    'visible' => false
134
                ],
135
                [
136
                    'label' => 'Dropdown1',
137
                    'items' => [
138
                        ['label' => 'Page2', 'content' => 'Page2'],
139
                        ['label' => 'InvisibleItem', 'content' => 'Invisible Item Content', 'visible' => false],
140
                        ['label' => 'Page3', 'content' => 'Page3'],
141
                        ['label' => 'External Link', 'url' => ['//other/dropdown/route']],
142
                        ['label' => 'Invisible External Link', 'url' => ['//other/dropdown/route'], 'visible' => false],
143
                    ]
144
                ],
145
            ])
146
            ->render();
147
148
        $this-> assertStringNotContainsString('InvisiblePage', $html);
149
        $this-> assertStringNotContainsString('Invisible Page Content', $html);
150
        $this-> assertStringNotContainsString('InvisibleItem', $html);
151
        $this-> assertStringNotContainsString('Invisible Item Content', $html);
152
        $this-> assertStringNotContainsString('Invisible External Link', $html);
153
    }
154
155
    public function testDisabled(): void
156
    {
157
        Tabs::counter(0);
158
159
        $html = Tabs::widget()
160
            ->items([
161
                [
162
                    'label' => 'Page1',
163
                    'content' => 'Page1',
164
                    'disabled' => true
165
                ],
166
                [
167
                    'label' => 'Page2',
168
                    'content' => 'Page2',
169
                ],
170
                [
171
                    'label' => 'DisabledPage',
172
                    'content' => 'Disabled Page Content',
173
                    'disabled' => true
174
                ],
175
                [
176
                    'label' => 'Dropdown1',
177
                    'items' => [
178
                        ['label' => 'Page2', 'content' => 'Page2'],
179
                        ['label' => 'DisabledItem', 'content' => 'Disabled Item Content', 'disabled' => true],
180
                        ['label' => 'Page3', 'content' => 'Page3'],
181
                        ['label' => 'External Link', 'url' => '/other/dropdown/route'],
182
                        ['label' => 'Disabled External Link', 'url' => '/other/dropdown/route', 'disabled' => true],
183
                    ]
184
                ],
185
            ])
186
            ->render();
187
188
        $this->assertStringContainsString(
189
            '<li class="nav-item"><a class="nav-link disabled" href="#w0-tabs-tab0" data-toggle="tab" role="tab" aria-controls="w0-tabs-tab0" tabindex="-1" aria-disabled="true">Page1</a></li>',
190
            $html
191
        );
192
        $this->assertStringContainsString(
193
            '<li class="nav-item"><a class="nav-link active" href="#w0-tabs-tab1" data-toggle="tab" role="tab" aria-controls="w0-tabs-tab1" aria-selected="true">Page2</a></li>',
194
            $html
195
        );
196
        $this->assertStringContainsString(
197
            '<li class="nav-item"><a class="nav-link disabled" href="#w0-tabs-tab2" data-toggle="tab" role="tab" aria-controls="w0-tabs-tab2" tabindex="-1" aria-disabled="true">DisabledPage</a></li>',
198
            $html
199
        );
200
        $this->assertStringContainsString(
201
            '<a class="dropdown-item disabled" href="#w0-tabs-dd3-tab1" data-toggle="tab" role="tab" aria-controls="w0-tabs-dd3-tab1" tabindex="-1" aria-disabled="true">DisabledItem</a>',
202
            $html
203
        );
204
        $this->assertStringContainsString(
205
            '<a class="dropdown-item disabled" href="/other/dropdown/route" tabindex="-1" aria-disabled="true">Disabled External Link</a>',
206
            $html
207
        );
208
    }
209
210
    public function testItem(): void
211
    {
212
        $checkTag = 'article';
213
214
        Tabs::counter(0);
215
216
        $html = Tabs::widget()
217
            ->items([
218
                [
219
                    'label' => 'Page1',
220
                    'content' => 'Page1',
221
                ],
222
                [
223
                    'label' => 'Page2',
224
                    'content' => 'Page2',
225
                ],
226
            ])
227
            ->itemOptions(['tag' => $checkTag])
228
            ->renderTabContent(true)
229
            ->render();
230
231
        $this->assertStringContainsString('<' . $checkTag, $html);
232
    }
233
234
    public function testTabContentOptions(): void
235
    {
236
        $checkAttribute = 'test_attribute';
237
        $checkValue = 'check_attribute';
238
239
        Tabs::counter(0);
240
241
        $html = Tabs::widget()
242
            ->items([
243
                [
244
                    'label' => 'Page1',
245
                    'content' => 'Page1'
246
                ]
247
            ])
248
            ->tabContentOptions([
249
                $checkAttribute => $checkValue
250
            ])
251
            ->render();
252
253
        $this->assertStringContainsString($checkAttribute . '=', $html);
254
        $this->assertStringContainsString($checkValue, $html);
255
    }
256
257
    public function testActivateFirstVisibleTab(): void
258
    {
259
        Tabs::counter(0);
260
261
        $html = Tabs::widget()
262
            ->items([
263
                [
264
                    'label' => 'Tab 1',
265
                    'content' => 'some content',
266
                    'visible' => false
267
                ],
268
                [
269
                    'label' => 'Tab 2',
270
                    'content' => 'some content',
271
                    'disabled' => true
272
                ],
273
                [
274
                    'label' => 'Tab 3',
275
                    'content' => 'some content'
276
                ],
277
                [
278
                    'label' => 'Tab 4',
279
                    'content' => 'some content'
280
                ]
281
            ])
282
            ->options(['id' => 'mytab'])
283
            ->render();
284
285
        $this-> assertStringNotContainsString(
286
            '<li class="nav-item"><a class="nav-link active" href="#mytab-tab0" data-toggle="tab" role="tab" aria-controls="mytab-tab0" aria-selected="true">Tab 1</a></li>',
287
            $html
288
        );
289
        $this-> assertStringNotContainsString(
290
            '<li class="nav-item"><a class="nav-link active" href="#mytab-tab1" data-toggle="tab" role="tab" aria-controls="mytab-tab1" aria-selected="true">Tab 2</a></li>',
291
            $html
292
        );
293
        $this->assertStringContainsString(
294
            '<li class="nav-item"><a class="nav-link active" href="#mytab-tab2" data-toggle="tab" role="tab" aria-controls="mytab-tab2" aria-selected="true">Tab 3</a></li>',
295
            $html
296
        );
297
    }
298
299
    public function testActivateTab(): void
300
    {
301
        Tabs::counter(0);
302
303
        $html = Tabs::widget()
304
            ->items([
305
                [
306
                    'label' => 'Tab 1',
307
                    'content' => 'some content',
308
                    'visible' => false
309
                ],
310
                [
311
                    'label' => 'Tab 2',
312
                    'content' => 'some content'
313
                ],
314
                [
315
                    'label' => 'Tab 3',
316
                    'content' => 'some content',
317
                    'active' => true
318
                ],
319
                [
320
                    'label' => 'Tab 4',
321
                    'content' => 'some content'
322
                ]
323
            ])
324
            ->options(['id' => 'mytab'])
325
            ->render();
326
327
        $this->assertStringContainsString(
328
            '<li class="nav-item"><a class="nav-link active" href="#mytab-tab2" data-toggle="tab" role="tab" aria-controls="mytab-tab2" aria-selected="true">Tab 3</a></li>',
329
            $html
330
        );
331
    }
332
333
    public function testTabLabelEncoding(): void
334
    {
335
        Tabs::counter(0);
336
337
        $html = Tabs::widget()
338
            ->encodeLabels(false)
339
            ->setId('mytab')
340
            ->items([
341
                [
342
                    'label' => 'Tab 1<span>encoded</span>',
343
                    'content' => 'some content',
344
                    'encode' => true,
345
                ],
346
                [
347
                    'label' => 'Tab 2<span>not encoded</span>',
348
                    'content' => 'some content',
349
                ],
350
                [
351
                    'label' => 'Tab 3<span>not encoded too</span>',
352
                    'content' => 'some content',
353
                ],
354
            ])
355
            ->render();
356
357
        $this->assertStringContainsString('&lt;span&gt;encoded&lt;/span&gt;', $html);
358
        $this->assertStringContainsString('<span>not encoded</span>', $html);
359
        $this->assertStringContainsString('<span>not encoded too</span>', $html);
360
    }
361
362
    /**
363
     * {@see https://github.com/yiisoft/yii2-bootstrap4/issues/108#issuecomment-465219339}
364
     */
365
    public function testIdRendering(): void
366
    {
367
        Tabs::counter(0);
368
369
        $html = Tabs::widget()
370
            ->items([
371
                [
372
                    'options' => ['id' => 'pane1'],
373
                    'label' => 'Tab 1',
374
                    'content' => '<div>Content 1</div>',
375
                ],
376
                [
377
                    'label' => 'Tab 2',
378
                    'content' => '<div>Content 2</div>',
379
                ],
380
            ])
381
            ->render();
382
383
        $expected = <<<HTML
384
<ul id="w0-tabs" class="nav nav-tabs" role="tablist"><li class="nav-item"><a class="nav-link active" href="#pane1" data-toggle="tab" role="tab" aria-controls="pane1" aria-selected="true">Tab 1</a></li>
385
<li class="nav-item"><a class="nav-link" href="#w0-tabs-tab1" data-toggle="tab" role="tab" aria-controls="w0-tabs-tab1" aria-selected="false">Tab 2</a></li></ul>
386
<div class="tab-content"><div id="pane1" class="tab-pane active"><div>Content 1</div></div>
387
<div id="w0-tabs-tab1" class="tab-pane"><div>Content 2</div></div></div>
388
HTML;
389
390
        $this->assertEqualsWithoutLE($expected, $html);
391
    }
392
}
393