a7f5116f35ac8902a146831f5fcfe09e2bf6b471
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / FieldEntityLinkTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\simpletest\UserCreationTrait;
8 use Drupal\user\Entity\Role;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\views\Views;
11
12 /**
13  * Tests the core Drupal\views\Plugin\views\field\EntityOperations handler.
14  *
15  * @group views
16  */
17 class FieldEntityLinkTest extends ViewsKernelTestBase {
18
19   use UserCreationTrait;
20
21   /**
22    * Views used by this test.
23    *
24    * @var array
25    */
26   public static $testViews = ['test_entity_test_link'];
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['user', 'entity_test'];
34
35   /**
36    * An admin user account.
37    *
38    * @var \Drupal\user\UserInterface
39    */
40   protected $adminUser;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUpFixtures() {
46     parent::setUpFixtures();
47
48     $this->installEntitySchema('user');
49     $this->installEntitySchema('entity_test');
50     $this->installConfig(['user']);
51
52     // Create some test entities.
53     for ($i = 0; $i < 5; $i++) {
54       EntityTest::create(['name' => $this->randomString()])->save();
55     }
56
57     // Create and admin user.
58     $this->adminUser = $this->createUser(['view test entity'], FALSE, TRUE);
59
60     Role::load(AccountInterface::ANONYMOUS_ROLE)
61       ->grantPermission('view test entity')
62       ->save();
63   }
64
65   /**
66    * Tests entity link fields.
67    */
68   public function testEntityLink() {
69     // Anonymous users cannot see edit/delete links.
70     $expected_results = ['canonical' => TRUE, 'edit-form' => FALSE, 'delete-form' => FALSE, 'canonical_raw' => TRUE, 'canonical_raw_absolute' => TRUE];
71     $this->doTestEntityLink(\Drupal::currentUser(), $expected_results);
72
73     // Admin users cannot see all links.
74     $expected_results = ['canonical' => TRUE, 'edit-form' => TRUE, 'delete-form' => TRUE, 'canonical_raw' => TRUE, 'canonical_raw_absolute' => TRUE];
75     $this->doTestEntityLink($this->adminUser, $expected_results);
76   }
77
78   /**
79    * Tests whether entity links behave as expected.
80    *
81    * @param \Drupal\Core\Session\AccountInterface $account
82    *   The user account to be used to run the test;
83    * @param bool[] $expected_results
84    *   An associative array of expected results keyed by link template name.
85    */
86   protected function doTestEntityLink(AccountInterface $account, $expected_results) {
87     \Drupal::currentUser()->setAccount($account);
88
89     $view = Views::getView('test_entity_test_link');
90     $view->preview();
91
92     $info = [
93       'canonical' => [
94         'label' => 'View entity test',
95         'field_id' => 'view_entity_test',
96         'destination' => FALSE,
97         'link' => TRUE,
98         'options' => [],
99         'relationship' => 'canonical',
100       ],
101       'edit-form' => [
102         'label' => 'Edit entity test',
103         'field_id' => 'edit_entity_test',
104         'destination' => TRUE,
105         'link' => TRUE,
106         'options' => [],
107         'relationship' => 'edit-form',
108       ],
109       'delete-form' => [
110         'label' => 'Delete entity test',
111         'field_id' => 'delete_entity_test',
112         'destination' => TRUE,
113         'link' => TRUE,
114         'options' => [],
115         'relationship' => 'delete-form',
116       ],
117       'canonical_raw' => [
118         'field_id' => 'canonical_entity_test',
119         'destination' => FALSE,
120         'link' => FALSE,
121         'options' => [],
122         'relationship' => 'canonical',
123       ],
124       'canonical_raw_absolute' => [
125         'field_id' => 'absolute_entity_test',
126         'destination' => FALSE,
127         'link' => FALSE,
128         'options' => ['absolute' => TRUE],
129         'relationship' => 'canonical',
130       ],
131     ];
132
133     $index = 0;
134     foreach (EntityTest::loadMultiple() as $entity) {
135       foreach ($expected_results as $template => $expected_result) {
136         $expected_link = '';
137         if ($expected_result) {
138           $path = $entity->url($info[$template]['relationship'], $info[$template]['options']);
139           $destination = $info[$template]['destination'] ? '?destination=/' : '';
140           if ($info[$template]['link']) {
141             $expected_link = '<a href="' . $path . $destination . '" hreflang="en">' . $info[$template]['label'] . '</a>';
142           }
143           else {
144             $expected_link = $path;
145           }
146         }
147         $link = $view->style_plugin->getField($index, $info[$template]['field_id']);
148         $this->assertEqual($link, $expected_link);
149       }
150       $index++;
151     }
152   }
153
154 }