diff --git a/tests/unit/framework/widgets/BreadcrumbsTest.php b/tests/unit/framework/widgets/BreadcrumbsTest.php new file mode 100644 index 0000000..db64215 --- /dev/null +++ b/tests/unit/framework/widgets/BreadcrumbsTest.php @@ -0,0 +1,128 @@ + + * + * @group widgets + */ +class BreadcrumbsTest extends \yiiunit\TestCase +{ + private $breadcrumbs; + private $app; + + public function setUp() + { + $this->app = $this->mockApplication(); + Yii::setAlias('@testWeb', '/'); + Yii::setAlias('@testWebRoot', '@yiiunit/data/web'); + $this->breadcrumbs = new Breadcrumbs(); + } + + 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 = ""; + + 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 = ""; + + 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-
Test-Label']; + $method = $this->reflectMethod(); + $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + + $this->assertEquals("
  • My-<br>Test-Label
  • \n", $encodedValue); + + //without encodeLabels + $this->breadcrumbs->encodeLabels = false; + $unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + + $this->assertEquals("
  • My-
    Test-Label
  • \n", $unencodedValue); + } + + public function testRenderItemWithLabelAndUrl() + { + $link = ['label' => 'My-
    Test-Label', 'url' => 'http://localhost/yii2']; + $method = $this->reflectMethod(); + $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + + $this->assertEquals("
  • My-<br>Test-Label
  • \n", $encodedValue); + + // without encodeLabels + $this->breadcrumbs->encodeLabels = false; + $unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + $this->assertEquals("
  • My-
    Test-Label
  • \n", $unencodedValue); + } + + public function testRenderItemTemplate() + { + $link = ['label' => 'My-
    Test-Label', 'url' => 'http://localhost/yii2', 'template' => "{link}\n"]; + $method = $this->reflectMethod(); + $encodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + + $this->assertEquals("My-<br>Test-Label\n", $encodedValue); + + // without encodeLabels + $this->breadcrumbs->encodeLabels = false; + $unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); + $this->assertEquals("My-
    Test-Label
    \n", $unencodedValue); + } + + /** + * Helper methods + */ + protected function reflectMethod($class = '\yii\widgets\Breadcrumbs', $method = 'renderItem') + { + $value = new \ReflectionMethod($class, $method); + $value->setAccessible(true); + + return $value; + } +}