You are here

public function ForumNodeBreadcrumbBuilderTest::testBuild in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumNodeBreadcrumbBuilderTest::testBuild()

Tests ForumNodeBreadcrumbBuilder::build().

@covers ::build

See also

\Drupal\forum\ForumNodeBreadcrumbBuilder::build()

File

core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php, line 129
Contains \Drupal\Tests\forum\Unit\Breadcrumb\ForumNodeBreadcrumbBuilderTest.

Class

ForumNodeBreadcrumbBuilderTest
@coversDefaultClass \Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder @group forum

Namespace

Drupal\Tests\forum\Unit\Breadcrumb

Code

public function testBuild() {

  // Build all our dependencies, backwards.
  $prophecy = $this
    ->prophesize('Drupal\\taxonomy\\Entity\\Term');
  $prophecy
    ->label()
    ->willReturn('Something');
  $prophecy
    ->id()
    ->willReturn(1);
  $prophecy
    ->getCacheTags()
    ->willReturn([
    'taxonomy_term:1',
  ]);
  $prophecy
    ->getCacheContexts()
    ->willReturn([]);
  $prophecy
    ->getCacheMaxAge()
    ->willReturn(Cache::PERMANENT);
  $term1 = $prophecy
    ->reveal();
  $prophecy = $this
    ->prophesize('Drupal\\taxonomy\\Entity\\Term');
  $prophecy
    ->label()
    ->willReturn('Something else');
  $prophecy
    ->id()
    ->willReturn(2);
  $prophecy
    ->getCacheTags()
    ->willReturn([
    'taxonomy_term:2',
  ]);
  $prophecy
    ->getCacheContexts()
    ->willReturn([]);
  $prophecy
    ->getCacheMaxAge()
    ->willReturn(Cache::PERMANENT);
  $term2 = $prophecy
    ->reveal();
  $forum_manager = $this
    ->getMockBuilder('Drupal\\forum\\ForumManagerInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $forum_manager
    ->expects($this
    ->at(0))
    ->method('getParents')
    ->will($this
    ->returnValue(array(
    $term1,
  )));
  $forum_manager
    ->expects($this
    ->at(1))
    ->method('getParents')
    ->will($this
    ->returnValue(array(
    $term1,
    $term2,
  )));
  $prophecy = $this
    ->prophesize('Drupal\\taxonomy\\VocabularyInterface');
  $prophecy
    ->label()
    ->willReturn('Forums');
  $prophecy
    ->id()
    ->willReturn(5);
  $prophecy
    ->getCacheTags()
    ->willReturn([
    'taxonomy_vocabulary:5',
  ]);
  $prophecy
    ->getCacheContexts()
    ->willReturn([]);
  $prophecy
    ->getCacheMaxAge()
    ->willReturn(Cache::PERMANENT);
  $vocab_storage = $this
    ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $vocab_storage
    ->expects($this
    ->any())
    ->method('load')
    ->will($this
    ->returnValueMap(array(
    array(
      'forums',
      $prophecy
        ->reveal(),
    ),
  )));
  $entity_manager = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\EntityManagerInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $entity_manager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->will($this
    ->returnValueMap(array(
    array(
      'taxonomy_vocabulary',
      $vocab_storage,
    ),
  )));
  $config_factory = $this
    ->getConfigFactoryStub(array(
    'forum.settings' => array(
      'vocabulary' => 'forums',
    ),
  ));

  // Build a breadcrumb builder to test.
  $breadcrumb_builder = $this
    ->getMock('Drupal\\forum\\Breadcrumb\\ForumNodeBreadcrumbBuilder', NULL, array(
    $entity_manager,
    $config_factory,
    $forum_manager,
  ));

  // Add a translation manager for t().
  $translation_manager = $this
    ->getStringTranslationStub();
  $property = new \ReflectionProperty('Drupal\\forum\\Breadcrumb\\ForumNodeBreadcrumbBuilder', 'stringTranslation');
  $property
    ->setAccessible(TRUE);
  $property
    ->setValue($breadcrumb_builder, $translation_manager);

  // The forum node we need a breadcrumb back from.
  $forum_node = $this
    ->getMockBuilder('Drupal\\node\\Entity\\Node')
    ->disableOriginalConstructor()
    ->getMock();

  // Our data set.
  $route_match = $this
    ->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
  $route_match
    ->expects($this
    ->exactly(2))
    ->method('getParameter')
    ->with('node')
    ->will($this
    ->returnValue($forum_node));

  // First test.
  $expected1 = array(
    Link::createFromRoute('Home', '<front>'),
    Link::createFromRoute('Forums', 'forum.index'),
    Link::createFromRoute('Something', 'forum.page', array(
      'taxonomy_term' => 1,
    )),
  );
  $breadcrumb = $breadcrumb_builder
    ->build($route_match);
  $this
    ->assertEquals($expected1, $breadcrumb
    ->getLinks());
  $this
    ->assertEquals([
    'route',
  ], $breadcrumb
    ->getCacheContexts());
  $this
    ->assertEquals([
    'taxonomy_term:1',
    'taxonomy_vocabulary:5',
  ], $breadcrumb
    ->getCacheTags());
  $this
    ->assertEquals(Cache::PERMANENT, $breadcrumb
    ->getCacheMaxAge());

  // Second test.
  $expected2 = array(
    Link::createFromRoute('Home', '<front>'),
    Link::createFromRoute('Forums', 'forum.index'),
    Link::createFromRoute('Something else', 'forum.page', array(
      'taxonomy_term' => 2,
    )),
    Link::createFromRoute('Something', 'forum.page', array(
      'taxonomy_term' => 1,
    )),
  );
  $breadcrumb = $breadcrumb_builder
    ->build($route_match);
  $this
    ->assertEquals($expected2, $breadcrumb
    ->getLinks());
  $this
    ->assertEquals([
    'route',
  ], $breadcrumb
    ->getCacheContexts());
  $this
    ->assertEquals([
    'taxonomy_term:1',
    'taxonomy_term:2',
    'taxonomy_vocabulary:5',
  ], $breadcrumb
    ->getCacheTags());
  $this
    ->assertEquals(Cache::PERMANENT, $breadcrumb
    ->getCacheMaxAge());
}