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];
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];
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 'label' => 'Edit entity test',
100 'field_id' => 'edit_entity_test',
101 'destination' => TRUE,
104 'label' => 'Delete entity test',
105 'field_id' => 'delete_entity_test',
106 'destination' => TRUE,
111 foreach (EntityTest::loadMultiple() as $entity) {
112 foreach ($expected_results as $template => $expected_result) {
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>';
119 $link = $view->style_plugin->getField($index, $info[$template]['field_id']);
120 $this->assertEqual($link, $expected_link);