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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\Views\TaxonomyTermArgumentTest uses TaxonomyTestTrait
Expanded class hierarchy of TaxonomyTermArgumentTest
File
- tests/
src/ Unit/ Views/ TaxonomyTermArgumentTest.php, line 16
Namespace
Drupal\Tests\search_api\Unit\ViewsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
TaxonomyTermArgumentTest:: |
protected | function | Creates the plugin to test. | |
TaxonomyTermArgumentTest:: |
public | function |
Overrides UnitTestCase:: |
|
TaxonomyTermArgumentTest:: |
public | function | Tests that the set argument is returned when non existing ids are provided. | |
TaxonomyTermArgumentTest:: |
public | function | Tests that the set argument is returned when no value is provided. | |
TaxonomyTermArgumentTest:: |
public | function | Tests that a comma separated list of term labels is returned. | |
TaxonomyTermArgumentTest:: |
public | function | Tests that null is returned if no argument has been set for any reason. | |
TaxonomyTermArgumentTest:: |
public | function | Tests that the term label is returned if an existing id is provided. | |
TaxonomyTestTrait:: |
protected | property | The test container. | |
TaxonomyTestTrait:: |
protected | property | The mock entity repository service. | |
TaxonomyTestTrait:: |
protected | property | The mock term storage. | |
TaxonomyTestTrait:: |
public | function | Sets up the container with necessary services. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed 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. |