Verified Commit 9eda7251 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3439493 by mondrake: Change remaining Forum module test dataproviders to static

(cherry picked from commit df1f7b71)
parent aeb80208
Loading
Loading
Loading
Loading
Loading
+20 −33
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
@@ -47,12 +48,21 @@ protected function setUp(): void {
   * @dataProvider providerTestApplies
   * @covers ::applies
   */
  public function testApplies($expected, $route_name = NULL, $parameter_map = []) {
  public function testApplies(bool $expected, ?string $route_name = NULL, array $parameter_map = []): void {
    // Make some test doubles.
    $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
    $config_factory = $this->getConfigFactoryStub([]);
    $forum_manager = $this->createMock('Drupal\forum\ForumManagerInterface');
    $translation_manager = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface');
    $map = [];
    if ($parameter_map) {
      foreach ($parameter_map as $parameter) {
        $map[] = [
          $parameter[0],
          $parameter[1] === TRUE ? $this->getMockBuilder(Term::class)->disableOriginalConstructor()->getMock() : $parameter[1],
        ];
      }
    }

    // Make an object to test.
    $builder = new ForumListingBreadcrumbBuilder($entity_type_manager, $config_factory, $forum_manager, $translation_manager);
@@ -63,7 +73,7 @@ public function testApplies($expected, $route_name = NULL, $parameter_map = [])
      ->willReturn($route_name);
    $route_match->expects($this->any())
      ->method('getParameter')
      ->willReturnMap($parameter_map);
      ->willReturnMap($map);

    $this->assertEquals($expected, $builder->applies($route_match));
  }
@@ -71,40 +81,17 @@ public function testApplies($expected, $route_name = NULL, $parameter_map = [])
  /**
   * Provides test data for testApplies().
   *
   * @return array
   *   Array of datasets for testApplies(). Structured as such:
   * @return \Generator
   *   Datasets for testApplies(). Structured as such:
   *   - ForumListBreadcrumbBuilder::applies() expected result.
   *   - ForumListBreadcrumbBuilder::applies() $attributes input array.
   */
  public function providerTestApplies() {
    // Send a Node mock, because NodeInterface cannot be mocked.
    $mock_term = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
      ->disableOriginalConstructor()
      ->getMock();

    return [
      [
        FALSE,
      ],
      [
        FALSE,
        'NOT.forum.page',
      ],
      [
        FALSE,
        'forum.page',
      ],
      [
        TRUE,
        'forum.page',
        [['taxonomy_term', 'anything']],
      ],
      [
        TRUE,
        'forum.page',
        [['taxonomy_term', $mock_term]],
      ],
    ];
  public static function providerTestApplies(): \Generator {
    yield [FALSE];
    yield [FALSE, 'NOT.forum.page'];
    yield [FALSE, 'forum.page'];
    yield [TRUE, 'forum.page', [['taxonomy_term', 'anything']]];
    yield [TRUE, 'forum.page', [['taxonomy_term', TRUE]]];
  }

  /**
+20 −33
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
@@ -47,10 +48,19 @@ protected function setUp(): void {
   * @dataProvider providerTestApplies
   * @covers ::applies
   */
  public function testApplies($expected, $route_name = NULL, $parameter_map = []) {
  public function testApplies(bool $expected, ?string $route_name = NULL, array $parameter_map = []): void {
    // Make some test doubles.
    $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
    $config_factory = $this->getConfigFactoryStub([]);
    $map = [];
    if ($parameter_map) {
      foreach ($parameter_map as $parameter) {
        $map[] = [
          $parameter[0],
          $parameter[1] === TRUE ? $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock() : $parameter[1],
        ];
      }
    }

    $forum_manager = $this->createMock('Drupal\forum\ForumManagerInterface');
    $forum_manager->expects($this->any())
@@ -68,7 +78,7 @@ public function testApplies($expected, $route_name = NULL, $parameter_map = [])
      ->willReturn($route_name);
    $route_match->expects($this->any())
      ->method('getParameter')
      ->willReturnMap($parameter_map);
      ->willReturnMap($map);

    $this->assertEquals($expected, $builder->applies($route_match));
  }
@@ -78,40 +88,17 @@ public function testApplies($expected, $route_name = NULL, $parameter_map = [])
   *
   * Note that this test is incomplete, because we can't mock NodeInterface.
   *
   * @return array
   *   Array of datasets for testApplies(). Structured as such:
   * @return \Generator
   *   Datasets for testApplies(). Structured as such:
   *   - ForumNodeBreadcrumbBuilder::applies() expected result.
   *   - ForumNodeBreadcrumbBuilder::applies() $attributes input array.
   */
  public function providerTestApplies() {
    // Send a Node mock, because NodeInterface cannot be mocked.
    $mock_node = $this->getMockBuilder('Drupal\node\Entity\Node')
      ->disableOriginalConstructor()
      ->getMock();

    return [
      [
        FALSE,
      ],
      [
        FALSE,
        'NOT.entity.node.canonical',
      ],
      [
        FALSE,
        'entity.node.canonical',
      ],
      [
        FALSE,
        'entity.node.canonical',
        [['node', NULL]],
      ],
      [
        TRUE,
        'entity.node.canonical',
        [['node', $mock_node]],
      ],
    ];
  public static function providerTestApplies(): \Generator {
    yield [FALSE];
    yield [FALSE, 'NOT.entity.node.canonical'];
    yield [FALSE, 'entity.node.canonical'];
    yield [FALSE, 'entity.node.canonical', [['node', NULL]]];
    yield [TRUE, 'entity.node.canonical', [['node', TRUE]]];
  }

  /**