FormElementTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
B testFormElement() 0 24 1
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
}
75