0a4ac415bd75e4a0858a95d77a5a26d7b3d0846f
[yaffs-website] / Tests / FormElementTest.php
1 <?php
2
3 namespace Drupal\entity_browser\Tests;
4
5 use Drupal\entity_browser\Element\EntityBrowserElement;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the entity browser form element.
10  *
11  * @group entity_browser
12  */
13 class FormElementTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['entity_browser_test', 'node', 'views'];
21
22   /**
23    * Test nodes.
24    *
25    * @var \Drupal\node\NodeInterface[]
26    */
27   protected $nodes;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34     $this->container
35       ->get('entity_type.manager')
36       ->getStorage('node_type')
37       ->create([
38         'type' => 'page',
39         'name' => 'page',
40       ])->save();
41
42     $this->nodes[] = $this->drupalCreateNode();
43     $this->nodes[] = $this->drupalCreateNode();
44   }
45
46   /**
47    * Tests the Entity browser form element.
48    */
49   public function testFormElement() {
50     $this->drupalGet('/test-element');
51     $this->assertLink('Select entities', 0, 'Trigger link found.');
52     $this->assertFieldByXPath("//input[@type='hidden' and @id='edit-fancy-entity-browser-target']", '', "Entity browser's hidden element found.");
53
54     $edit = [
55       'fancy_entity_browser[entity_ids]' => $this->nodes[0]->getEntityTypeId() . ':' . $this->nodes[0]->id() . ' ' . $this->nodes[1]->getEntityTypeId() . ':' . $this->nodes[1]->id(),
56     ];
57     $this->drupalPostForm(NULL, $edit, 'Submit');
58     $expected = 'Selected entities: ' . $this->nodes[0]->label() . ', ' . $this->nodes[1]->label();
59     $this->assertText($expected, 'Selected entities detected.');
60
61     $default_entity = $this->nodes[0]->getEntityTypeId() . ':' . $this->nodes[0]->id();
62     $this->drupalGet('/test-element', ['query' => ['default_entity' => $default_entity, 'selection_mode' => EntityBrowserElement::SELECTION_MODE_EDIT]]);
63     $this->assertLink('Select entities', 0, 'Trigger link found.');
64     $this->assertFieldByXPath("//input[@type='hidden' and @id='edit-fancy-entity-browser-target']", $default_entity, "Entity browser's hidden element found.");
65
66     $edit = [
67       'fancy_entity_browser[entity_ids]' => $this->nodes[1]->getEntityTypeId() . ':' . $this->nodes[1]->id() . ' ' . $this->nodes[0]->getEntityTypeId() . ':' . $this->nodes[0]->id(),
68     ];
69     $this->drupalPostForm(NULL, $edit, 'Submit');
70     $expected = 'Selected entities: ' . $this->nodes[1]->label() . ', ' . $this->nodes[0]->label();
71     $this->assertText($expected, 'Selected entities detected.');
72   }
73
74 }