Version 1
[yaffs-website] / web / core / modules / block / tests / src / Unit / CategoryAutocompleteTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Unit;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\block\Controller\CategoryAutocompleteController;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9
10 /**
11  * @coversDefaultClass \Drupal\block\Controller\CategoryAutocompleteController
12  * @group block
13  */
14 class CategoryAutocompleteTest extends UnitTestCase {
15
16   /**
17    * The autocomplete controller.
18    *
19    * @var \Drupal\block\Controller\CategoryAutocompleteController
20    */
21   protected $autocompleteController;
22
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']));
28
29     $this->autocompleteController = new CategoryAutocompleteController($block_manager);
30   }
31
32   /**
33    * Tests the autocomplete method.
34    *
35    * @param string $string
36    *   The string entered into the autocomplete.
37    * @param array $suggestions
38    *   The array of expected suggestions.
39    *
40    * @see \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()
41    *
42    * @dataProvider providerTestAutocompleteSuggestions
43    */
44   public function testAutocompleteSuggestions($string, $suggestions) {
45     $suggestions = array_map(function ($suggestion) {
46       return ['value' => $suggestion, 'label' => Html::escape($suggestion)];
47     }, $suggestions);
48     $result = $this->autocompleteController->autocomplete(new Request(['q' => $string]));
49     $this->assertSame($suggestions, json_decode($result->getContent(), TRUE));
50   }
51
52   /**
53    * Data provider for testAutocompleteSuggestions().
54    *
55    * @return array
56    */
57   public function providerTestAutocompleteSuggestions() {
58     $test_parameters = [];
59     $test_parameters[] = [
60       'string' => 'Com',
61       'suggestions' => [
62         'Comment',
63       ],
64     ];
65     $test_parameters[] = [
66       'string' => 'No',
67       'suggestions' => [
68         'Node',
69         'None & Such',
70       ],
71     ];
72     $test_parameters[] = [
73       'string' => 'us',
74       'suggestions' => [
75         'User',
76       ],
77     ];
78     $test_parameters[] = [
79       'string' => 'Banana',
80       'suggestions' => [],
81     ];
82     return $test_parameters;
83   }
84
85 }