Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Views / TaxonomyDefaultArgumentTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Views;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\views\Views;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\HttpKernelInterface;
9
10 /**
11  * Tests the representative node relationship for terms.
12  *
13  * @group taxonomy
14  */
15 class TaxonomyDefaultArgumentTest extends TaxonomyTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['taxonomy_default_argument_test'];
23
24   /**
25    * Init view with a request by provided url.
26    *
27    * @param string $request_url
28    *   The requested url.
29    * @param string $view_name
30    *   The name of the view.
31    *
32    * @return \Drupal\views\ViewExecutable
33    *   The initiated view.
34    *
35    * @throws \Exception
36    */
37   protected function initViewWithRequest($request_url, $view_name = 'taxonomy_default_argument_test') {
38     $view = Views::getView($view_name);
39
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');
43
44     $response = $this->container->get('http_kernel')
45       ->handle($request, HttpKernelInterface::SUB_REQUEST);
46
47     $view->setRequest($request);
48     $view->setResponse($response);
49     $view->initHandlers();
50
51     return $view;
52   }
53
54   /**
55    * Tests the relationship.
56    */
57   public function testNodePath() {
58     $view = $this->initViewWithRequest($this->nodes[0]->url());
59
60     $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
61     $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
62     $view->destroy();
63   }
64
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']);
68
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', [
73       'view' => [
74         'view_name' => 'test_entity_reference',
75         'display_name' => 'entity_reference_1',
76       ],
77     ]);
78     $field->save();
79
80     $view = $this->initViewWithRequest($this->nodes[0]->url());
81
82     $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
83     $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
84   }
85
86   public function testTermPath() {
87     $view = $this->initViewWithRequest($this->term1->url());
88
89     $expected = $this->term1->id();
90     $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
91   }
92
93 }