Version 1
[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];
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];
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       ],
98       'edit-form' => [
99         'label' => 'Edit entity test',
100         'field_id' => 'edit_entity_test',
101         'destination' => TRUE,
102       ],
103       'delete-form' => [
104         'label' => 'Delete entity test',
105         'field_id' => 'delete_entity_test',
106         'destination' => TRUE,
107       ],
108     ];
109
110     $index = 0;
111     foreach (EntityTest::loadMultiple() as $entity) {
112       foreach ($expected_results as $template => $expected_result) {
113         $expected_link = '';
114         if ($expected_result) {
115           $path = $entity->url($template);
116           $destination = $info[$template]['destination'] ? '?destination=/' : '';
117           $expected_link = '<a href="' . $path . $destination . '" hreflang="en">' . $info[$template]['label'] . '</a>';
118         }
119         $link = $view->style_plugin->getField($index, $info[$template]['field_id']);
120         $this->assertEqual($link, $expected_link);
121       }
122       $index++;
123     }
124   }
125
126 }