1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace yiiunit\framework\widgets;
use Yii;
use yii\widgets\Breadcrumbs;
/**
* @author Nelson J Morais <njmorais@gmail.com>
*
* @group widgets
*/
class BreadcrumbsTest extends \yiiunit\TestCase
{
private $breadcrumbs;
public function setUp()
{
// dirty way to have Request object not throwing exception when running testHomeLinkNull()
$_SERVER['SCRIPT_FILENAME'] = "/index.php";
$_SERVER['SCRIPT_NAME'] = "/index.php";
$this->mockWebApplication();
$this->breadcrumbs = new Breadcrumbs();
}
public function testHomeLinkNull()
{
$this->breadcrumbs->homeLink = null;
$this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
$expectedHtml = "<ul class=\"breadcrumb\"><li><a href=\"/index.php\">Home</a></li>\n"
. "<li class=\"active\">My Home Page</li>\n"
. "<li class=\"active\">http://my.example.com/yii2/link/page</li>\n"
. "</ul>";
ob_start();
$this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();
$this->assertEquals($expectedHtml, $actualHtml);
}
public function testEmptyLinks()
{
$this->assertNull($this->breadcrumbs->run());
}
public function testHomeLinkFalse()
{
$this->breadcrumbs->homeLink = false;
$this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
$expectedHtml = "<ul class=\"breadcrumb\"><li class=\"active\">My Home Page</li>\n"
. "<li class=\"active\">http://my.example.com/yii2/link/page</li>\n"
. "</ul>";
ob_start();
$this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();
$this->assertEquals($expectedHtml, $actualHtml);
}
public function testHomeLink()
{
$this->breadcrumbs->homeLink = ['label' => 'home-link'];
$this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
$expectedHtml = "<ul class=\"breadcrumb\"><li>home-link</li>\n"
. "<li class=\"active\">My Home Page</li>\n"
. "<li class=\"active\">http://my.example.com/yii2/link/page</li>\n"
. "</ul>";
ob_start();
$this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();
$this->assertEquals($expectedHtml, $actualHtml);
}
public function testRenderItemException()
{
$link = ['url' => 'http://localhost/yii2'];
$method = $this->reflectMethod();
$this->setExpectedException('yii\base\InvalidConfigException');
$method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
}
public function testRenderItemLabelOnly()
{
$link = ['label' => 'My-<br>Test-Label'];
$method = $this->reflectMethod();
$encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<li>My-<br>Test-Label</li>\n", $encodedValue);
//without encodeLabels
$this->breadcrumbs->encodeLabels = false;
$unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<li>My-<br>Test-Label</li>\n", $unencodedValue);
}
public function testRenderItemWithLabelAndUrl()
{
$link = ['label' => 'My-<br>Test-Label', 'url' => 'http://localhost/yii2'];
$method = $this->reflectMethod();
$encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<li><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></li>\n", $encodedValue);
// without encodeLabels
$this->breadcrumbs->encodeLabels = false;
$unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<li><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></li>\n", $unencodedValue);
}
public function testRenderItemTemplate()
{
$link = ['label' => 'My-<br>Test-Label', 'url' => 'http://localhost/yii2', 'template' => "<td>{link}</td>\n"];
$method = $this->reflectMethod();
$encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<td><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></td>\n", $encodedValue);
// without encodeLabels
$this->breadcrumbs->encodeLabels = false;
$unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<td><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></td>\n", $unencodedValue);
}
/**
* Helper methods
*/
protected function reflectMethod($class = '\yii\widgets\Breadcrumbs', $method = 'renderItem')
{
$value = new \ReflectionMethod($class, $method);
$value->setAccessible(true);
return $value;
}
}