View source  
  <?php
namespace Drupal\Tests\facets\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Entity\Facet;
use Drupal\facets\FacetSourceInterface;
use Drupal\views\Views;
class UrlIntegrationTest extends FacetsTestBase {
  
  public static $modules = [
    'views',
    'node',
    'search_api',
    'facets',
    'block',
    'facets_search_api_dependency',
    'facets_query_processor',
  ];
  
  public function setUp() {
    parent::setUp();
    $this
      ->drupalLogin($this->adminUser);
    $this
      ->setUpExampleStructure();
    $this
      ->insertExampleContent();
    $this
      ->assertEquals($this
      ->indexItems($this->indexId), 5, '5 items were indexed.');
  }
  
  public function testUrlIntegration() {
    $id = 'facet';
    $name = '&^Facet@#1';
    $this
      ->createFacet($name, $id);
    $url = Url::fromUserInput('/search-api-test-fulltext', [
      'query' => [
        'f[0]' => 'facet:item',
      ],
    ]);
    $this
      ->checkClickedFacetUrl($url);
    
    $facet = Facet::load($id);
    $this
      ->assertInstanceOf(FacetInterface::class, $facet);
    $config = $facet
      ->getFacetSourceConfig();
    $this
      ->assertInstanceOf(FacetSourceInterface::class, $config);
    $this
      ->assertEquals('f', $config
      ->getFilterKey());
    $facet = NULL;
    $config = NULL;
    
    $this
      ->drupalGet('admin/config/search/facets');
    $this
      ->clickLink('Configure', 1);
    $edit = [
      'filter_key' => 'y',
      'url_processor' => 'query_string',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Save');
    
    $facet = Facet::load($id);
    $config = $facet
      ->getFacetSourceConfig();
    $this
      ->assertInstanceOf(FacetSourceInterface::class, $config);
    $this
      ->assertEquals('y', $config
      ->getFilterKey());
    $facet = NULL;
    $config = NULL;
    $url_2 = Url::fromUserInput('/search-api-test-fulltext', [
      'query' => [
        'y[0]' => 'facet:item',
      ],
    ]);
    $this
      ->checkClickedFacetUrl($url_2);
    
    $this
      ->drupalGet('admin/config/search/facets');
    $this
      ->clickLink('Configure', 1);
    $edit = [
      'filter_key' => 'y',
      'url_processor' => 'dummy_query',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Save');
    
    $facet = Facet::load($id);
    $config = $facet
      ->getFacetSourceConfig();
    $this
      ->assertInstanceOf(FacetSourceInterface::class, $config);
    $this
      ->assertEquals('y', $config
      ->getFilterKey());
    $facet = NULL;
    $config = NULL;
    $url_3 = Url::fromUserInput('/search-api-test-fulltext', [
      'query' => [
        'y[0]' => 'facet||item',
      ],
    ]);
    $this
      ->checkClickedFacetUrl($url_3);
  }
  
  public function testColonValue() {
    $id = 'water_bear';
    $name = 'Water bear';
    $this
      ->createFacet($name, $id, 'keywords');
    
    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');
    $entity_test_storage
      ->create([
      'name' => 'Entity with colon',
      'body' => 'test test',
      'type' => 'item',
      'keywords' => [
        'orange',
        'test:colon',
      ],
      'category' => 'item_category',
    ])
      ->save();
    
    $this
      ->assertEquals(1, $this
      ->indexItems($this->indexId));
    
    $this
      ->drupalGet('search-api-test-fulltext');
    $this
      ->assertFacetLabel('test:colon');
    $this
      ->assertFacetLabel('orange');
    $this
      ->assertFacetLabel('banana');
    
    $this
      ->clickLink('test:colon');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    
    $url = Url::fromUserInput('/search-api-test-fulltext', [
      'query' => [
        'f[0]' => 'water_bear:test:colon',
      ],
    ]);
    $this
      ->assertSession()
      ->addressEquals($url);
    $this
      ->checkFacetIsActive('test:colon');
    $this
      ->assertFacetLabel('orange');
    $this
      ->assertFacetLabel('banana');
  }
  
  public function testIncompleteFacetUrl() {
    $id = 'owl';
    $name = 'Owl';
    $this
      ->createFacet($name, $id);
    $url = Url::fromUserInput('/search-api-test-fulltext');
    $this
      ->checkClickedFacetUrl($url);
    
    $path = 'search-api-test-fulltext';
    $options['absolute'] = TRUE;
    $url = $this
      ->buildUrl($path, $options);
    $url .= '?f';
    
    $session = $this
      ->getSession();
    $this
      ->prepareRequest();
    $session
      ->visit($url);
    
    $this
      ->assertSession()
      ->statusCodeEquals(200);
  }
  
  public function testResetPager() {
    $id = 'owl';
    $name = 'Owl';
    $this
      ->createFacet($name, $id);
    
    $view = Views::getView('search_api_test_view');
    $view
      ->setDisplay('page_1');
    $pagerOptions = $view->display_handler
      ->getOption('pager');
    $pagerOptions['options']['items_per_page'] = 2;
    $view->display_handler
      ->setOption('pager', $pagerOptions);
    $view
      ->save();
    $content_types = [
      'item',
      'article',
    ];
    foreach ($content_types as $content_type) {
      $this
        ->drupalGet('search-api-test-fulltext');
      $this
        ->clickLink('2');
      $this
        ->assertNotFalse(strpos($this
        ->getUrl(), 'page=1'));
      $this
        ->clickLink($content_type);
      $this
        ->assertFalse(strpos($this
        ->getUrl(), 'page=1'));
    }
  }
  
  public function testCreatingDuplicateUrlAlias() {
    $this
      ->createFacet('Owl', 'owl');
    $this
      ->createFacet('Another owl', 'another_owl');
    $this
      ->drupalGet('admin/config/search/facets/another_owl/edit');
    $this
      ->drupalPostForm(NULL, [
      'facet_settings[url_alias]' => 'owl',
    ], 'Save');
    $this
      ->assertSession()
      ->pageTextContains('This alias is already in use for another facet defined on the same source.');
  }
  
  public function testFacetUrlCanBeChanged() {
    $modules = [
      'facets_events_test',
    ];
    $success = $this->container
      ->get('module_installer')
      ->install($modules, TRUE);
    $this
      ->assertTrue($success, new FormattableMarkup('Enabled modules: %modules', [
      '%modules' => implode(', ', $modules),
    ]));
    $this
      ->rebuildAll();
    $id = 'facet';
    $name = 'Facet';
    $this
      ->createFacet($name, $id);
    $url = Url::fromUserInput('/search-api-test-fulltext', [
      'query' => [
        'f[0]' => 'facet:item',
        'test' => 'fun',
      ],
    ]);
    $this
      ->checkClickedFacetUrl($url);
  }
}