BreadcrumbsTest.php 6.08 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace yiiunit\framework\widgets;

use Yii;
use yii\widgets\Breadcrumbs;

/**
 * @author Nelson J Morais <njmorais@gmail.com>
10
 *
11 12 13 14 15
 * @group widgets
 */
class BreadcrumbsTest extends \yiiunit\TestCase
{
    private $breadcrumbs;
16

17 18
    public function setUp()
    {
19
        // dirty way to have Request object not throwing exception when running testHomeLinkNull()
Qiang Xue committed
20 21
        $_SERVER['SCRIPT_FILENAME'] = "/index.php";
        $_SERVER['SCRIPT_NAME'] = "/index.php";
22

Qiang Xue committed
23
        $this->mockWebApplication();
24 25
        $this->breadcrumbs = new Breadcrumbs();
    }
26

27 28 29 30
    public function testHomeLinkNull()
    {
        $this->breadcrumbs->homeLink = null;
        $this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
31

Qiang Xue committed
32
        $expectedHtml = "<ul class=\"breadcrumb\"><li><a href=\"/index.php\">Home</a></li>\n"
33 34 35
            . "<li class=\"active\">My Home Page</li>\n"
            . "<li class=\"active\">http://my.example.com/yii2/link/page</li>\n"
            . "</ul>";
36

37 38 39 40
        ob_start();
        $this->breadcrumbs->run();
        $actualHtml = ob_get_contents();
        ob_end_clean();
41 42

        $this->assertEquals($expectedHtml, $actualHtml);
43
    }
44

45 46 47 48
    public function testEmptyLinks()
    {
        $this->assertNull($this->breadcrumbs->run());
    }
49

50 51 52 53
    public function testHomeLinkFalse()
    {
        $this->breadcrumbs->homeLink = false;
        $this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
54

55 56 57
        $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>";
58

59 60 61 62
        ob_start();
        $this->breadcrumbs->run();
        $actualHtml = ob_get_contents();
        ob_end_clean();
63

64 65 66
        $this->assertEquals($expectedHtml, $actualHtml);
    }

67

68
    public function testHomeLink()
69
    {
70 71
        $this->breadcrumbs->homeLink = ['label' => 'home-link'];
        $this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
72

73 74 75 76
        $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>";
77

78 79 80 81
        ob_start();
        $this->breadcrumbs->run();
        $actualHtml = ob_get_contents();
        ob_end_clean();
82

83 84
        $this->assertEquals($expectedHtml, $actualHtml);
    }
85

86 87 88 89 90 91 92
    public function testRenderItemException()
    {
        $link = ['url' => 'http://localhost/yii2'];
        $method = $this->reflectMethod();
        $this->setExpectedException('yii\base\InvalidConfigException');
        $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
    }
93

94 95 96 97
    public function testRenderItemLabelOnly()
    {
        $link = ['label' => 'My-<br>Test-Label'];
        $method = $this->reflectMethod();
98 99
        $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);

100
        $this->assertEquals("<li>My-&lt;br&gt;Test-Label</li>\n", $encodedValue);
101

102 103 104
        //without encodeLabels
        $this->breadcrumbs->encodeLabels = false;
        $unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
105

106 107
        $this->assertEquals("<li>My-<br>Test-Label</li>\n", $unencodedValue);
    }
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    public function testEncodeOverride()
    {
        $link = ['label' => 'My-<br>Test-Label', 'encode' => false];
        $method = $this->reflectMethod();
        $result = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);

        $this->assertEquals("<li>My-<br>Test-Label</li>\n", $result);

        //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);
    }

124 125 126 127
    public function testRenderItemWithLabelAndUrl()
    {
        $link = ['label' => 'My-<br>Test-Label', 'url' => 'http://localhost/yii2'];
        $method = $this->reflectMethod();
128 129
        $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);

130
        $this->assertEquals("<li><a href=\"http://localhost/yii2\">My-&lt;br&gt;Test-Label</a></li>\n", $encodedValue);
131

132
        // without encodeLabels
133 134
        $this->breadcrumbs->encodeLabels = false;
        $unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
135 136
        $this->assertEquals("<li><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></li>\n", $unencodedValue);
    }
137

138 139 140 141
    public function testRenderItemTemplate()
    {
        $link = ['label' => 'My-<br>Test-Label', 'url' => 'http://localhost/yii2', 'template' => "<td>{link}</td>\n"];
        $method = $this->reflectMethod();
142 143
        $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);

144
        $this->assertEquals("<td><a href=\"http://localhost/yii2\">My-&lt;br&gt;Test-Label</a></td>\n", $encodedValue);
145

146
        // without encodeLabels
147 148 149
        $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);
150
    }
151 152 153 154 155 156 157 158 159 160 161 162

    public function testExtraOptions()
    {
        $link = [
            'label' => 'demo',
            'url' => 'http://example.com',
            'class' => 'external',
        ];
        $method = $this->reflectMethod();
        $result = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
        $this->assertEquals('<li><a class="external" href="http://example.com">demo</a></li>' . "\n", $result);
    }
163

164 165 166 167 168 169 170
    /**
     * Helper methods
     */
    protected function reflectMethod($class = '\yii\widgets\Breadcrumbs', $method = 'renderItem')
    {
        $value = new \ReflectionMethod($class, $method);
        $value->setAccessible(true);
171

172 173 174
        return $value;
    }
}