You are here

class TaxonomyTermArgumentTest in Search API 8

Tests whether the SearchApiTerm argument plugin works correctly.

@group search_api

@coversDefaultClass \Drupal\search_api\Plugin\views\argument\SearchApiTerm

Hierarchy

Expanded class hierarchy of TaxonomyTermArgumentTest

File

tests/src/Unit/Views/TaxonomyTermArgumentTest.php, line 16

Namespace

Drupal\Tests\search_api\Unit\Views
View source
class TaxonomyTermArgumentTest extends UnitTestCase {
  use TaxonomyTestTrait;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this
      ->setupContainer();
  }

  /**
   * Tests that null is returned if no argument has been set for any reason.
   *
   * @covers ::title
   */
  public function testReturnsNullIfArgumentNotSet() {
    $plugin = $this
      ->getSubjectUnderTest();
    $this
      ->assertNull($plugin
      ->title());
  }

  /**
   * Tests that the set argument is returned when no value is provided.
   *
   * @covers ::title
   */
  public function testReturnsArgumentIfSet() {
    $plugin = $this
      ->getSubjectUnderTest('argument');
    $plugin->value = [];
    $this
      ->assertEquals('argument', $plugin
      ->title());
  }

  /**
   * Tests that the set argument is returned when non existing ids are provided.
   *
   * @covers ::title
   */
  public function testReturnsArgumentIfInvalidTermIdIsPassed() {
    $plugin = $this
      ->getSubjectUnderTest('argument');
    $prophecy = $this
      ->prophesize(Term::class);
    $prophecy
      ->label()
      ->willReturn('First');
    $prophecy
      ->id()
      ->willReturn(1);
    $term = $prophecy
      ->reveal();
    $non_existing_term_id = $term
      ->id() + 1;
    $this->termStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with($non_existing_term_id)
      ->willReturn(NULL);
    $plugin->value = [
      $non_existing_term_id,
    ];
    $this
      ->assertEquals('argument', $plugin
      ->title());
  }

  /**
   * Tests that the term label is returned if an existing id is provided.
   *
   * @covers ::title
   */
  public function testReturnsTermNameIfValidTermIdIsPassed() {
    $plugin = $this
      ->getSubjectUnderTest('argument');
    $prophecy = $this
      ->prophesize(Term::class);
    $prophecy
      ->label()
      ->willReturn('First');
    $prophecy
      ->id()
      ->willReturn(1);
    $term = $prophecy
      ->reveal();
    $this->termStorage
      ->expects($this
      ->any())
      ->method('load')
      ->with($term
      ->id())
      ->willReturn($term);
    $this->entityRepository
      ->expects($this
      ->any())
      ->method('getTranslationFromContext')
      ->with($term)
      ->will($this
      ->returnValue($term));
    $plugin->value = [
      $term
        ->id(),
    ];
    $this
      ->assertEquals($term
      ->label(), $plugin
      ->title());
  }

  /**
   * Tests that a comma separated list of term labels is returned.
   *
   * @covers ::title
   */
  public function testReturnsCommaSeparatedNamesIfValidTermIdsArePassed() {
    $plugin = $this
      ->getSubjectUnderTest('argument');
    $prophecy = $this
      ->prophesize(Term::class);
    $prophecy
      ->label()
      ->willReturn('First');
    $prophecy
      ->id()
      ->willReturn(1);
    $term1 = $prophecy
      ->reveal();
    $prophecy = $this
      ->prophesize(Term::class);
    $prophecy
      ->label()
      ->willReturn('Second');
    $prophecy
      ->id()
      ->willReturn(2);
    $term2 = $prophecy
      ->reveal();
    $this->termStorage
      ->expects($this
      ->at(0))
      ->method('load')
      ->with($term1
      ->id())
      ->willReturn($term1);
    $this->termStorage
      ->expects($this
      ->at(1))
      ->method('load')
      ->with($term2
      ->id())
      ->willReturn($term2);
    $this->entityRepository
      ->expects($this
      ->at(0))
      ->method('getTranslationFromContext')
      ->with($term1)
      ->will($this
      ->returnValue($term1));
    $this->entityRepository
      ->expects($this
      ->at(1))
      ->method('getTranslationFromContext')
      ->with($term2)
      ->will($this
      ->returnValue($term2));
    $plugin->value = [
      $term1
        ->id(),
      $term2
        ->id(),
    ];
    $this
      ->assertEquals("{$term1->label()}, {$term2->label()}", $plugin
      ->title());
  }

  /**
   * Creates the plugin to test.
   *
   * @param string|null $argument
   *   The argument to set on the plugin.
   *
   * @return \Drupal\search_api\Plugin\views\argument\SearchApiTerm
   *   The subject under test.
   */
  protected function getSubjectUnderTest($argument = NULL) {
    $plugin = new SearchApiTerm([], 'search_api_term', []);
    if ($argument !== NULL) {
      $plugin->argument_validated = TRUE;
      $plugin
        ->setArgument($argument);
    }
    return $plugin;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TaxonomyTermArgumentTest::getSubjectUnderTest protected function Creates the plugin to test.
TaxonomyTermArgumentTest::setUp public function Overrides UnitTestCase::setUp
TaxonomyTermArgumentTest::testReturnsArgumentIfInvalidTermIdIsPassed public function Tests that the set argument is returned when non existing ids are provided.
TaxonomyTermArgumentTest::testReturnsArgumentIfSet public function Tests that the set argument is returned when no value is provided.
TaxonomyTermArgumentTest::testReturnsCommaSeparatedNamesIfValidTermIdsArePassed public function Tests that a comma separated list of term labels is returned.
TaxonomyTermArgumentTest::testReturnsNullIfArgumentNotSet public function Tests that null is returned if no argument has been set for any reason.
TaxonomyTermArgumentTest::testReturnsTermNameIfValidTermIdIsPassed public function Tests that the term label is returned if an existing id is provided.
TaxonomyTestTrait::$container protected property The test container.
TaxonomyTestTrait::$entityRepository protected property The mock entity repository service.
TaxonomyTestTrait::$termStorage protected property The mock term storage.
TaxonomyTestTrait::setupContainer public function Sets up the container with necessary services.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.