Completed
Push — 8.x-1.x ( f23998...729d7d )
by Janez
02:46
created

FormElementTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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