3 namespace Drupal\Tests\views\Functional\Handler;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\Tests\views\Functional\ViewTestBase;
11 * Tests the core Drupal\views\Plugin\views\field\EntityOperations handler.
15 class FieldEntityOperationsTest extends ViewTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_entity_operations'];
29 public static $modules = ['node', 'language', 'views_ui'];
31 protected function setUp($import_test_views = TRUE) {
32 parent::setUp($import_test_views);
34 // Create Article content type.
35 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
39 * Tests entity operations field.
41 public function testEntityOperations() {
42 // Add languages and refresh the container so the entity manager will have
44 ConfigurableLanguage::createFromLangcode('hu')->save();
45 ConfigurableLanguage::createFromLangcode('es')->save();
46 $this->rebuildContainer();
48 // Create some test entities. Every other entity is Hungarian while all
49 // have a Spanish translation.
51 for ($i = 0; $i < 5; $i++) {
52 $entity = Node::create([
53 'title' => $this->randomString(),
55 'langcode' => $i % 2 === 0 ? 'hu' : 'en',
58 $translation = $entity->addTranslation('es');
59 $translation->set('title', $entity->getTitle() . ' in Spanish');
61 $entities[$i] = $entity;
64 $admin_user = $this->drupalCreateUser(['access administration pages', 'administer nodes', 'bypass node access']);
65 $this->drupalLogin($this->rootUser);
66 $this->drupalGet('test-entity-operations');
67 /** @var $entity \Drupal\entity_test\Entity\EntityTest */
68 foreach ($entities as $entity) {
69 /** @var \Drupal\Core\Language\LanguageInterface $language */
70 foreach ($entity->getTranslationLanguages() as $language) {
71 $entity = $entity->getTranslation($language->getId());
72 $operations = \Drupal::entityManager()->getListBuilder('node')->getOperations($entity);
73 $this->assertTrue(count($operations) > 0, 'There are operations.');
74 foreach ($operations as $operation) {
75 $expected_destination = Url::fromUri('internal:/test-entity-operations')->toString();
76 $result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[@href=:path and text()=:title]', [':path' => $operation['url']->toString() . '?destination=' . $expected_destination, ':title' => (string) $operation['title']]);
77 $this->assertEqual(count($result), 1, t('Found entity @operation link with destination parameter.', ['@operation' => $operation['title']]));
78 // Entities which were created in Hungarian should link to the Hungarian
79 // edit form, others to the English one (which has no path prefix here).
80 $base_path = \Drupal::request()->getBasePath();
81 $parts = explode('/', str_replace($base_path, '', $operation['url']->toString()));
82 $expected_prefix = ($language->getId() != 'en' ? $language->getId() : 'node');
83 $this->assertEqual($parts[1], $expected_prefix, 'Entity operation links to the correct language for the entity.');
88 // Test that we can't enable click sorting on the operation field.
89 $this->drupalGet('admin/structure/views/nojs/display/test_entity_operations/page_2/style_options');
90 $this->assertField('style_options[info][title][sortable]');
91 $this->assertNoField('style_options[info][operations][sortable]');