3 namespace Drupal\Tests\views\Kernel\Handler;
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;
13 * Tests the core Drupal\views\Plugin\views\field\EntityOperations handler.
17 class FieldEntityLinkTest extends ViewsKernelTestBase {
19 use UserCreationTrait;
22 * Views used by this test.
26 public static $testViews = ['test_entity_test_link'];
33 public static $modules = ['user', 'entity_test'];
36 * An admin user account.
38 * @var \Drupal\user\UserInterface
45 protected function setUpFixtures() {
46 parent::setUpFixtures();
48 $this->installEntitySchema('user');
49 $this->installEntitySchema('entity_test');
50 $this->installConfig(['user']);
52 // Create some test entities.
53 for ($i = 0; $i < 5; $i++) {
54 EntityTest::create(['name' => $this->randomString()])->save();
57 // Create and admin user.
58 $this->adminUser = $this->createUser(['view test entity'], FALSE, TRUE);
60 Role::load(AccountInterface::ANONYMOUS_ROLE)
61 ->grantPermission('view test entity')
66 * Tests entity link fields.
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);
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);
79 * Tests whether entity links behave as expected.
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.
86 protected function doTestEntityLink(AccountInterface $account, $expected_results) {
87 \Drupal::currentUser()->setAccount($account);
89 $view = Views::getView('test_entity_test_link');
94 'label' => 'View entity test',
95 'field_id' => 'view_entity_test',
96 'destination' => FALSE,
99 'relationship' => 'canonical',
102 'label' => 'Edit entity test',
103 'field_id' => 'edit_entity_test',
104 'destination' => TRUE,
107 'relationship' => 'edit-form',
110 'label' => 'Delete entity test',
111 'field_id' => 'delete_entity_test',
112 'destination' => TRUE,
115 'relationship' => 'delete-form',
118 'field_id' => 'canonical_entity_test',
119 'destination' => FALSE,
122 'relationship' => 'canonical',
124 'canonical_raw_absolute' => [
125 'field_id' => 'absolute_entity_test',
126 'destination' => FALSE,
128 'options' => ['absolute' => TRUE],
129 'relationship' => 'canonical',
134 foreach (EntityTest::loadMultiple() as $entity) {
135 foreach ($expected_results as $template => $expected_result) {
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>';
144 $expected_link = $path;
147 $link = $view->style_plugin->getField($index, $info[$template]['field_id']);
148 $this->assertEqual($link, $expected_link);