View source
<?php
namespace Drupal\Tests\search_api_autocomplete\Unit;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Url;
use Drupal\search_api_autocomplete\Suggestion\SuggestionFactory;
use Drupal\Tests\UnitTestCase;
class SuggestionFactoryTest extends UnitTestCase {
public function testCreateFromSuggestedKeys() {
$factory = new SuggestionFactory('foo');
$suggestion = $factory
->createFromSuggestedKeys('bar');
$this
->assertEquals('bar', $suggestion
->getSuggestedKeys());
$this
->assertNull($suggestion
->getUserInput());
$this
->assertEquals('bar', $suggestion
->getLabel());
$this
->assertNull($suggestion
->getResultsCount());
$suggestion = $factory
->createFromSuggestedKeys('fooo');
$this
->assertEquals('fooo', $suggestion
->getSuggestedKeys());
$this
->assertEquals('foo', $suggestion
->getUserInput());
$this
->assertNull($suggestion
->getLabel());
$this
->assertEquals('o', $suggestion
->getSuggestionSuffix());
$this
->assertNull($suggestion
->getResultsCount());
$suggestion = $factory
->createFromSuggestedKeys('foooo', 5);
$this
->assertEquals('foooo', $suggestion
->getSuggestedKeys());
$this
->assertEquals('foo', $suggestion
->getUserInput());
$this
->assertNull($suggestion
->getLabel());
$this
->assertEquals('oo', $suggestion
->getSuggestionSuffix());
$this
->assertEquals(5, $suggestion
->getResultsCount());
$suggestion = $factory
->createFromSuggestedKeys('Foooo', 5);
$this
->assertEquals('Foooo', $suggestion
->getSuggestedKeys());
$this
->assertEquals('Foo', $suggestion
->getUserInput());
$this
->assertNull($suggestion
->getLabel());
$this
->assertEquals('oo', $suggestion
->getSuggestionSuffix());
$this
->assertEquals(5, $suggestion
->getResultsCount());
Unicode::check();
$factory = new SuggestionFactory('öd');
$suggestion = $factory
->createFromSuggestedKeys('Ödön', 5);
$this
->assertEquals('Ödön', $suggestion
->getSuggestedKeys());
$this
->assertEquals('Öd', $suggestion
->getUserInput());
$this
->assertNull($suggestion
->getLabel());
$this
->assertEquals('ön', $suggestion
->getSuggestionSuffix());
$this
->assertEquals(5, $suggestion
->getResultsCount());
}
public function testCreateFromSuggestionSuffix() {
$factory = new SuggestionFactory('foo');
$suggestion = $factory
->createFromSuggestionSuffix('bar');
$this
->assertEquals('foobar', $suggestion
->getSuggestedKeys());
$this
->assertEquals('foo', $suggestion
->getUserInput());
$this
->assertEquals('bar', $suggestion
->getSuggestionSuffix());
$this
->assertNull($suggestion
->getLabel());
$this
->assertNull($suggestion
->getResultsCount());
$suggestion = $factory
->createFromSuggestionSuffix('o', 5);
$this
->assertEquals('fooo', $suggestion
->getSuggestedKeys());
$this
->assertEquals('foo', $suggestion
->getUserInput());
$this
->assertEquals('o', $suggestion
->getSuggestionSuffix());
$this
->assertNull($suggestion
->getLabel());
$this
->assertEquals(5, $suggestion
->getResultsCount());
}
public function testCreateUrlSuggestion() {
$factory = new SuggestionFactory('foo');
$url = $this
->getMockBuilder(Url::class)
->disableOriginalConstructor()
->getMock();
$suggestion = $factory
->createUrlSuggestion($url, 'Foobar');
$this
->assertSame($url, $suggestion
->getUrl());
$this
->assertNull($suggestion
->getSuggestedKeys());
$this
->assertNull($suggestion
->getUserInput());
$this
->assertEquals('Foobar', $suggestion
->getLabel());
$this
->assertNull($suggestion
->getResultsCount());
$this
->assertNull($suggestion
->getRender());
$render = [
'foo' => 'bar',
];
$suggestion = $factory
->createUrlSuggestion($url, NULL, $render);
$this
->assertSame($url, $suggestion
->getUrl());
$this
->assertNull($suggestion
->getSuggestedKeys());
$this
->assertNull($suggestion
->getUserInput());
$this
->assertNull($suggestion
->getLabel());
$this
->assertNull($suggestion
->getResultsCount());
$this
->assertEquals($render, $suggestion
->getRender());
}
}