class BreadcrumbManagerTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest
@coversDefaultClass \Drupal\Core\Breadcrumb\BreadcrumbManager @group Breadcrumb
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest
Expanded class hierarchy of BreadcrumbManagerTest
File
- core/
tests/ Drupal/ Tests/ Core/ Breadcrumb/ BreadcrumbManagerTest.php, line 21 - Contains \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest.
Namespace
Drupal\Tests\Core\BreadcrumbView source
class BreadcrumbManagerTest extends UnitTestCase {
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* The breadcrumb object.
*
* @var \Drupal\Core\Breadcrumb\Breadcrumb
*/
protected $breadcrumb;
/**
* The tested breadcrumb manager.
*
* @var \Drupal\Core\Breadcrumb\BreadcrumbManager
*/
protected $breadcrumbManager;
/**
* The mocked module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->moduleHandler = $this
->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->breadcrumbManager = new BreadcrumbManager($this->moduleHandler);
$this->breadcrumb = new Breadcrumb();
$this->container = new ContainerBuilder();
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class);
$cache_contexts_manager
->assertValidTokens()
->willReturn(TRUE);
$cache_contexts_manager
->reveal();
$this->container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($this->container);
}
/**
* Tests the breadcrumb manager without any set breadcrumb.
*/
public function testBuildWithoutBuilder() {
$route_match = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('system_breadcrumb', $this->breadcrumb, $route_match, [
'builder' => NULL,
]);
$breadcrumb = $this->breadcrumbManager
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
$this
->assertEquals([], $breadcrumb
->getLinks());
$this
->assertEquals([], $breadcrumb
->getCacheContexts());
$this
->assertEquals([], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
/**
* Tests the build method with a single breadcrumb builder.
*/
public function testBuildWithSingleBuilder() {
$builder = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$links = array(
'<a href="/example">Test</a>',
);
$this->breadcrumb
->setLinks($links);
$this->breadcrumb
->addCacheContexts([
'foo',
])
->addCacheTags([
'bar',
]);
$builder
->expects($this
->once())
->method('applies')
->will($this
->returnValue(TRUE));
$builder
->expects($this
->once())
->method('build')
->willReturn($this->breadcrumb);
$route_match = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('system_breadcrumb', $this->breadcrumb, $route_match, array(
'builder' => $builder,
));
$this->breadcrumbManager
->addBuilder($builder, 0);
$breadcrumb = $this->breadcrumbManager
->build($route_match);
$this
->assertEquals($links, $breadcrumb
->getLinks());
$this
->assertEquals([
'foo',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([
'bar',
], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
/**
* Tests multiple breadcrumb builder with different priority.
*/
public function testBuildWithMultipleApplyingBuilders() {
$builder1 = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$builder1
->expects($this
->never())
->method('applies');
$builder1
->expects($this
->never())
->method('build');
$builder2 = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$links2 = array(
'<a href="/example2">Test2</a>',
);
$this->breadcrumb
->setLinks($links2);
$this->breadcrumb
->addCacheContexts([
'baz',
])
->addCacheTags([
'qux',
]);
$builder2
->expects($this
->once())
->method('applies')
->will($this
->returnValue(TRUE));
$builder2
->expects($this
->once())
->method('build')
->willReturn($this->breadcrumb);
$route_match = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('system_breadcrumb', $this->breadcrumb, $route_match, array(
'builder' => $builder2,
));
$this->breadcrumbManager
->addBuilder($builder1, 0);
$this->breadcrumbManager
->addBuilder($builder2, 10);
$breadcrumb = $this->breadcrumbManager
->build($route_match);
$this
->assertEquals($links2, $breadcrumb
->getLinks());
$this
->assertEquals([
'baz',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([
'qux',
], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
/**
* Tests multiple breadcrumb builders of which one returns NULL.
*/
public function testBuildWithOneNotApplyingBuilders() {
$builder1 = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$builder1
->expects($this
->once())
->method('applies')
->will($this
->returnValue(FALSE));
$builder1
->expects($this
->never())
->method('build');
$builder2 = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$links2 = [
'<a href="/example2">Test2</a>',
];
$this->breadcrumb
->setLinks($links2);
$this->breadcrumb
->addCacheContexts([
'baz',
])
->addCacheTags([
'qux',
]);
$builder2
->expects($this
->once())
->method('applies')
->will($this
->returnValue(TRUE));
$builder2
->expects($this
->once())
->method('build')
->willReturn($this->breadcrumb);
$route_match = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('system_breadcrumb', $this->breadcrumb, $route_match, array(
'builder' => $builder2,
));
$this->breadcrumbManager
->addBuilder($builder1, 10);
$this->breadcrumbManager
->addBuilder($builder2, 0);
$breadcrumb = $this->breadcrumbManager
->build($route_match);
$this
->assertEquals($links2, $breadcrumb
->getLinks());
$this
->assertEquals([
'baz',
], $breadcrumb
->getCacheContexts());
$this
->assertEquals([
'qux',
], $breadcrumb
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $breadcrumb
->getCacheMaxAge());
}
/**
* Tests a breadcrumb builder with a bad return value.
*
* @expectedException \UnexpectedValueException
*/
public function testBuildWithInvalidBreadcrumbResult() {
$builder = $this
->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
$builder
->expects($this
->once())
->method('applies')
->will($this
->returnValue(TRUE));
$builder
->expects($this
->once())
->method('build')
->will($this
->returnValue('invalid_result'));
$this->breadcrumbManager
->addBuilder($builder, 0);
$this->breadcrumbManager
->build($this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BreadcrumbManagerTest:: |
protected | property | The breadcrumb object. | |
BreadcrumbManagerTest:: |
protected | property | The tested breadcrumb manager. | |
BreadcrumbManagerTest:: |
protected | property | The dependency injection container. | |
BreadcrumbManagerTest:: |
protected | property | The mocked module handler. | |
BreadcrumbManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
BreadcrumbManagerTest:: |
public | function | Tests a breadcrumb builder with a bad return value. | |
BreadcrumbManagerTest:: |
public | function | Tests multiple breadcrumb builder with different priority. | |
BreadcrumbManagerTest:: |
public | function | Tests multiple breadcrumb builders of which one returns NULL. | |
BreadcrumbManagerTest:: |
public | function | Tests the breadcrumb manager without any set breadcrumb. | |
BreadcrumbManagerTest:: |
public | function | Tests the build method with a single breadcrumb builder. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |