Completed
Push — 8.x-1.x ( 9760c0...1376e8 )
by Janez
07:47
created

testEntityEmbedButtonMarkup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_embed\Tests\EntityEmbedDialogTest.
6
 */
7
8
namespace Drupal\entity_embed\Tests;
9
10
use Drupal\editor\Entity\Editor;
11
12
/**
13
 * Tests the entity_embed dialog controller and route.
14
 *
15
 * @group entity_embed
16
 */
17
class EntityEmbedDialogTest extends EntityEmbedTestBase {
18
19
  /**
20
   * Tests the entity embed dialog.
21
   */
22
  public function testEntityEmbedDialog() {
23
    // Ensure that the route is not accessible without specifying all the
24
    // parameters.
25
    $this->getEmbedDialog();
26
    $this->assertResponse(404, 'Embed dialog is not accessible without specifying filter format and embed button.');
27
    $this->getEmbedDialog('custom_format');
28
    $this->assertResponse(404, 'Embed dialog is not accessible without specifying embed button.');
29
30
    // Ensure that the route is not accessible with an invalid embed button.
31
    $this->getEmbedDialog('custom_format', 'invalid_button');
32
    $this->assertResponse(404, 'Embed dialog is not accessible without specifying filter format and embed button.');
33
34
    // Ensure that the route is not accessible with text format without the
35
    // button configured.
36
    $this->getEmbedDialog('plain_text', 'node');
37
    $this->assertResponse(404, 'Embed dialog is not accessible with a filter that does not have an editor configuration.');
38
39
    // Add an empty configuration for the plain_text editor configuration.
40
    $editor = Editor::create([
41
      'format' => 'plain_text',
42
      'editor' => 'ckeditor',
43
    ]);
44
    $editor->save();
45
    $this->getEmbedDialog('plain_text', 'node');
46
    $this->assertResponse(403, 'Embed dialog is not accessible with a filter that does not have the embed button assigned to it.');
47
48
    // Ensure that the route is accessible with a valid embed button.
49
    // 'Node' embed button is provided by default by the module and hence the
50
    // request must be successful.
51
    $this->getEmbedDialog('custom_format', 'node');
52
    $this->assertResponse(200, 'Embed dialog is accessible with correct filter format and embed button.');
53
54
    // Ensure form structure of the 'select' step and submit form.
55
    $this->assertFieldByName('attributes[data-entity-id]', '', 'Entity ID/UUID field is present.');
56
57
    // $edit = ['attributes[data-entity-id]' => $this->node->id()];
58
    // $this->drupalPostAjaxForm(NULL, $edit, 'op');
59
    // Ensure form structure of the 'embed' step and submit form.
60
    // $this->assertFieldByName('attributes[data-entity-embed-display]', 'Entity Embed Display plugin field is present.');
61
  }
62
63
  /**
64
   * Tests the entity embed button markup.
65
   */
66
  public function testEntityEmbedButtonMarkup() {
67
    // Ensure that the route is not accessible with text format without the
68
    // button configured.
69
    $this->getEmbedDialog('plain_text', 'node');
70
    $this->assertResponse(404, 'Embed dialog is not accessible with a filter that does not have an editor configuration.');
71
72
    // Add an empty configuration for the plain_text editor configuration.
73
    $editor = Editor::create([
74
      'format' => 'plain_text',
75
      'editor' => 'ckeditor',
76
    ]);
77
    $editor->save();
78
    $this->getEmbedDialog('plain_text', 'node');
79
    $this->assertResponse(403, 'Embed dialog is not accessible with a filter that does not have the embed button assigned to it.');
80
81
    // Ensure that the route is accessible with a valid embed button.
82
    // 'Node' embed button is provided by default by the module and hence the
83
    // request must be successful.
84
    $this->getEmbedDialog('custom_format', 'node');
85
    $this->assertResponse(200, 'Embed dialog is accessible with correct filter format and embed button.');
86
87
    // Ensure form structure of the 'select' step and submit form.
88
    $this->assertFieldByName('attributes[data-entity-id]', '', 'Entity ID/UUID field is present.');
89
90
    // Check that 'Next' is a primary button.
91
    $this->assertFieldByXPath('//input[contains(@class, "button--primary")]', 'Next', 'Next is a primary button');
92
93
    $title =  $this->node->getTitle() . ' (' . $this->node->id() . ')';
94
    $edit = ['attributes[data-entity-id]' => $title];
95
    $this->drupalPostAjaxForm(NULL, $edit, 'op');
96
    /*$this->drupalPostForm(NULL, $edit, 'Next');
97
    // Ensure form structure of the 'embed' step and submit form.
98
    $this->assertFieldByName('attributes[data-entity-embed-display]', 'Entity Embed Display plugin field is present.');
99
100
    // Check that 'Embed' is a primary button.
101
    $this->assertFieldByXPath('//input[contains(@class, "button--primary")]', 'Embed', 'Embed is a primary button');*/
102
  }
103
104
  /**
105
   * Retrieves an embed dialog based on given parameters.
106
   *
107
   * @param string $filter_format_id
108
   *   ID of the filter format.
109
   * @param string $embed_button_id
110
   *   ID of the embed button.
111
   *
112
   * @return string
113
   *   The retrieved HTML string.
114
   */
115
  public function getEmbedDialog($filter_format_id = NULL, $embed_button_id = NULL) {
116
    $url = 'entity-embed/dialog';
117
    if (!empty($filter_format_id)) {
118
      $url .= '/' . $filter_format_id;
119
      if (!empty($embed_button_id)) {
120
        $url .= '/' . $embed_button_id;
121
      }
122
    }
123
    return $this->drupalGet($url);
124
  }
125
126
}
127