3 namespace Drupal\Tests\block\Unit;
5 use Drupal\Component\Utility\Html;
6 use Drupal\block\Controller\CategoryAutocompleteController;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
11 * @coversDefaultClass \Drupal\block\Controller\CategoryAutocompleteController
14 class CategoryAutocompleteTest extends UnitTestCase {
17 * The autocomplete controller.
19 * @var \Drupal\block\Controller\CategoryAutocompleteController
21 protected $autocompleteController;
23 protected function setUp() {
24 $block_manager = $this->getMock('Drupal\Core\Block\BlockManagerInterface');
25 $block_manager->expects($this->any())
26 ->method('getCategories')
27 ->will($this->returnValue(['Comment', 'Node', 'None & Such', 'User']));
29 $this->autocompleteController = new CategoryAutocompleteController($block_manager);
33 * Tests the autocomplete method.
35 * @param string $string
36 * The string entered into the autocomplete.
37 * @param array $suggestions
38 * The array of expected suggestions.
40 * @see \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()
42 * @dataProvider providerTestAutocompleteSuggestions
44 public function testAutocompleteSuggestions($string, $suggestions) {
45 $suggestions = array_map(function ($suggestion) {
46 return ['value' => $suggestion, 'label' => Html::escape($suggestion)];
48 $result = $this->autocompleteController->autocomplete(new Request(['q' => $string]));
49 $this->assertSame($suggestions, json_decode($result->getContent(), TRUE));
53 * Data provider for testAutocompleteSuggestions().
57 public function providerTestAutocompleteSuggestions() {
58 $test_parameters = [];
59 $test_parameters[] = [
65 $test_parameters[] = [
72 $test_parameters[] = [
78 $test_parameters[] = [
82 return $test_parameters;