summaryrefslogtreecommitdiffstats
path: root/src/Tests/UrlIntegrationTest.php
blob: 4d3107fe96fa24fe662e9c09ac3e931ff4be7893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

namespace Drupal\facets\Tests;

use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Entity\Facet;
use Drupal\facets\FacetSourceInterface;

/**
 * Tests the overall functionality of the Facets admin UI.
 *
 * @group facets
 */
class UrlIntegrationTest extends WebTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'views',
    'node',
    'search_api',
    'search_api_test_backend',
    'facets',
    'block',
    'facets_search_api_dependency',
    'facets_query_processor',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->drupalLogin($this->adminUser);

    $this->setUpExampleStructure();
    $this->insertExampleContent();
    $this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.');
  }

  /**
   * Tests various url integration things.
   */
  public function testUrlIntegration() {
    $id = 'facet';
    $name = '&^Facet@#1';
    $facet_add_page = 'admin/config/search/facets/add-facet';

    $this->drupalGet($facet_add_page);

    $form_values = [
      'id' => $id,
      'status' => 1,
      'name' => $name,
      'facet_source_id' => 'search_api_views:search_api_test_view:page_1',
      'facet_source_configs[search_api_views:search_api_test_view:page_1][field_identifier]' => 'type',
    ];
    $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_view:page_1'], $this->t('Configure facet source'));
    $this->drupalPostForm(NULL, $form_values, $this->t('Save'));

    $block_values = [
      'plugin_id' => 'facet_block:' . $id,
      'settings' => [
        'region' => 'footer',
        'id' => str_replace('_', '-', $id),
      ],
    ];
    $this->drupalPlaceBlock($block_values['plugin_id'], $block_values['settings']);

    $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'facet:item']]);
    $this->checkClickedFacetUrl($url);

    /** @var \Drupal\facets\FacetInterface $facet */
    $facet = Facet::load($id);
    $this->assertTrue($facet instanceof FacetInterface);
    $config = $facet->getFacetSourceConfig();
    $this->assertTrue($config instanceof FacetSourceInterface);
    $this->assertEqual(NULL, $config->getFilterKey());

    $facet = NULL;
    $config = NULL;

    // Go to the only enabled facet source's config and change the filter key.
    $this->drupalGet('admin/config/search/facets');
    $this->clickLink($this->t('Configure'), 1);

    $edit = [
      'filter_key' => 'y',
      'url_processor' => 'query_string',
    ];
    $this->drupalPostForm(NULL, $edit, $this->t('Save'));

    /** @var \Drupal\facets\FacetInterface $facet */
    $facet = Facet::load($id);
    $config = $facet->getFacetSourceConfig();
    $this->assertTrue($config instanceof FacetSourceInterface);
    $this->assertEqual('y', $config->getFilterKey());

    $facet = NULL;
    $config = NULL;

    $url_2 = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['y[0]' => 'facet:item']]);
    $this->checkClickedFacetUrl($url_2);

    // Go to the only enabled facet source's config and change the url
    // processor.
    $this->drupalGet('admin/config/search/facets');
    $this->clickLink($this->t('Configure'), 1);

    $edit = [
      'filter_key' => 'y',
      'url_processor' => 'dummy_query',
    ];
    $this->drupalPostForm(NULL, $edit, $this->t('Save'));

    /** @var \Drupal\facets\FacetInterface $facet */
    $facet = Facet::load($id);
    $config = $facet->getFacetSourceConfig();
    $this->assertTrue($config instanceof FacetSourceInterface);
    $this->assertEqual('y', $config->getFilterKey());

    $facet = NULL;
    $config = NULL;

    $url_3 = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['y[0]' => 'facet||item']]);
    $this->checkClickedFacetUrl($url_3);
  }

  /**
   * Tests the url when a colon is used in the value.
   */
  public function testColonValue() {
    $id = 'water_bear';
    $name = 'Water bear';

    $facet_add_page = 'admin/config/search/facets/add-facet';

    // Create the facet.
    $this->drupalGet($facet_add_page);

    $form_values = [
      'id' => $id,
      'status' => 1,
      'name' => $name,
      'facet_source_id' => 'search_api_views:search_api_test_view:page_1',
      'facet_source_configs[search_api_views:search_api_test_view:page_1][field_identifier]' => 'keywords',
    ];
    $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_view:page_1'], $this->t('Configure facet source'));
    $this->drupalPostForm(NULL, $form_values, $this->t('Save'));

    // Create / place the block.
    $block_settings = [
      'region' => 'footer',
      'id' => str_replace('_', '-', $id),
    ];
    $this->drupalPlaceBlock('facet_block:' . $id, $block_settings);

    // Add a new entity that has a colon in one of it's keywords.
    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test');
    $entity_test_storage->create([
      'name' => 'Entity with colon',
      'body' => 'test test',
      'type' => 'item',
      'keywords' => ['orange', 'test:colon'],
      'category' => 'item_category',
    ])->save();
    // Make sure the new item is indexed.
    $this->assertEqual(1, $this->indexItems($this->indexId));

    // Go to the overview and test that we have the expected links.
    $this->drupalGet('search-api-test-fulltext');
    $this->assertLink('test:colon');
    $this->assertLink('orange');
    $this->assertLink('banana');

    // Click the link with the colon.
    $this->clickLink('test:colon');
    $this->assertResponse(200);

    // Make sure 'test:colon' is active.
    $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'water_bear:test:colon']]);
    $this->assertUrl($url);
    $this->assertRaw('<span class="facet-deactivate">(-)</span> test:colon');
    $this->assertLink('orange');
    $this->assertLink('banana');
  }

  /**
   * Checks that the url after clicking a facet is as expected.
   *
   * @param \Drupal\Core\Url $url
   *   The expected url we end on.
   */
  protected function checkClickedFacetUrl(Url $url) {
    $this->drupalGet('search-api-test-fulltext');
    $this->assertResponse(200);
    $this->assertLink('item');
    $this->assertLink('article');

    $this->clickLink('item');

    $this->assertResponse(200);
    $this->assertRaw('<span class="facet-deactivate">(-)</span> item');
    $this->assertLink('article');
    $this->assertUrl($url);
  }

}