TaxonomyTermArgumentTest.php in Search API 8
File
tests/src/Unit/Views/TaxonomyTermArgumentTest.php
View source
<?php
namespace Drupal\Tests\search_api\Unit\Views;
use Drupal\search_api\Plugin\views\argument\SearchApiTerm;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\UnitTestCase;
class TaxonomyTermArgumentTest extends UnitTestCase {
use TaxonomyTestTrait;
public function setUp() {
parent::setUp();
$this
->setupContainer();
}
public function testReturnsNullIfArgumentNotSet() {
$plugin = $this
->getSubjectUnderTest();
$this
->assertNull($plugin
->title());
}
public function testReturnsArgumentIfSet() {
$plugin = $this
->getSubjectUnderTest('argument');
$plugin->value = [];
$this
->assertEquals('argument', $plugin
->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());
}
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());
}
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());
}
protected function getSubjectUnderTest($argument = NULL) {
$plugin = new SearchApiTerm([], 'search_api_term', []);
if ($argument !== NULL) {
$plugin->argument_validated = TRUE;
$plugin
->setArgument($argument);
}
return $plugin;
}
}