3 namespace Drupal\Tests\taxonomy\Kernel\Views;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\views\Views;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\HttpKernelInterface;
11 * Tests the representative node relationship for terms.
15 class TaxonomyDefaultArgumentTest extends TaxonomyTestBase {
18 * Views used by this test.
22 public static $testViews = ['taxonomy_default_argument_test'];
25 * Init view with a request by provided url.
27 * @param string $request_url
29 * @param string $view_name
30 * The name of the view.
32 * @return \Drupal\views\ViewExecutable
37 protected function initViewWithRequest($request_url, $view_name = 'taxonomy_default_argument_test') {
38 $view = Views::getView($view_name);
40 $request = Request::create($request_url);
41 $request->server->set('SCRIPT_NAME', $GLOBALS['base_path'] . 'index.php');
42 $request->server->set('SCRIPT_FILENAME', 'index.php');
44 $response = $this->container->get('http_kernel')
45 ->handle($request, HttpKernelInterface::SUB_REQUEST);
47 $view->setRequest($request);
48 $view->setResponse($response);
49 $view->initHandlers();
55 * Tests the relationship.
57 public function testNodePath() {
58 $view = $this->initViewWithRequest($this->nodes[0]->url());
60 $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
61 $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
65 public function testNodePathWithViewSelection() {
66 // Change the term entity reference field to use a view as selection plugin.
67 \Drupal::service('module_installer')->install(['entity_reference_test']);
69 $field_name = 'field_' . $this->vocabulary->id();
70 $field = FieldConfig::loadByName('node', 'article', $field_name);
71 $field->setSetting('handler', 'views');
72 $field->setSetting('handler_settings', [
74 'view_name' => 'test_entity_reference',
75 'display_name' => 'entity_reference_1',
80 $view = $this->initViewWithRequest($this->nodes[0]->url());
82 $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
83 $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
86 public function testTermPath() {
87 $view = $this->initViewWithRequest($this->term1->url());
89 $expected = $this->term1->id();
90 $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());