Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Handler / FieldEntityOperationsTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Handler;
4
5 use Drupal\Core\Url;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9
10 /**
11  * Tests the core Drupal\views\Plugin\views\field\EntityOperations handler.
12  *
13  * @group views
14  */
15 class FieldEntityOperationsTest extends ViewTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_entity_operations'];
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['node', 'language', 'views_ui'];
30
31   protected function setUp($import_test_views = TRUE) {
32     parent::setUp($import_test_views);
33
34     // Create Article content type.
35     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
36   }
37
38   /**
39    * Tests entity operations field.
40    */
41   public function testEntityOperations() {
42     // Add languages and refresh the container so the entity manager will have
43     // fresh data.
44     ConfigurableLanguage::createFromLangcode('hu')->save();
45     ConfigurableLanguage::createFromLangcode('es')->save();
46     $this->rebuildContainer();
47
48     // Create some test entities. Every other entity is Hungarian while all
49     // have a Spanish translation.
50     $entities = [];
51     for ($i = 0; $i < 5; $i++) {
52       $entity = Node::create([
53         'title' => $this->randomString(),
54         'type' => 'article',
55         'langcode' => $i % 2 === 0 ? 'hu' : 'en',
56       ]);
57       $entity->save();
58       $translation = $entity->addTranslation('es');
59       $translation->set('title', $entity->getTitle() . ' in Spanish');
60       $translation->save();
61       $entities[$i] = $entity;
62     }
63
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           // Update destination property of the URL as generating it in the
77           // test would by default point to the frontpage.
78           $operation['url']->setOption('query', ['destination' => $expected_destination]);
79           $result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[@href=:path and text()=:title]', [':path' => $operation['url']->toString(), ':title' => (string) $operation['title']]);
80           $this->assertEqual(count($result), 1, t('Found entity @operation link with destination parameter.', ['@operation' => $operation['title']]));
81           // Entities which were created in Hungarian should link to the Hungarian
82           // edit form, others to the English one (which has no path prefix here).
83           $base_path = \Drupal::request()->getBasePath();
84           $parts = explode('/', str_replace($base_path, '', $operation['url']->toString()));
85           $expected_prefix = ($language->getId() != 'en' ? $language->getId() : 'node');
86           $this->assertEqual($parts[1], $expected_prefix, 'Entity operation links to the correct language for the entity.');
87         }
88       }
89     }
90
91     // Test that we can't enable click sorting on the operation field.
92     $this->drupalGet('admin/structure/views/nojs/display/test_entity_operations/page_2/style_options');
93     $this->assertField('style_options[info][title][sortable]');
94     $this->assertNoField('style_options[info][operations][sortable]');
95   }
96
97 }