GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 8.x-2.x ( bdb6af...21ed67 )
by Devin
56:27
created

CkEditorContext::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Acquia\DFExtension\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use Drupal\Component\Serialization\Json;
7
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
8
9
/**
10
 * Contains step definitions for working with CKEditor instances.
11
 */
12
class CkEditorContext extends DrupalSubContextBase {
13
14
  /**
15
   * Asserts that a CKEditor instance exists and is fully loaded.
16
   *
17
   * @param string $id
18
   *   (optional) The editor instance ID. Defaults to the first available
19
   *   instance.
20
   *
21
   * @return string
22
   *   A snippet of JavaScript for calling instance methods.
23
   *
24
   * @Given CKEditor :id exists
25
   *
26
   * @Then CKEditor :id should exist
27
   */
28
  public function assertEditor($id = NULL) {
29
    $js = "CKEDITOR.instances['" . ($id ?: $this->getDefault()) . "']";
30
31
    $this->getSession()->wait(10000, "$js.status === 'ready'");
32
33
    return $js;
34
  }
35
36
  /**
37
   * Puts text or HTML into a CKEditor instance.
38
   *
39
   * @param string $text
40
   *   The text (or HTML) to insert into the editor.
41
   * @param string $id
42
   *   (optional) The editor instance ID.
43
   *
44
   * @When I put :text into CKEditor
45
   * @When I put :text into CKEditor :id
46
   */
47
  public function insert($text, $id = NULL) {
48
    $js = $this->assertEditor($id);
49
    $this->getSession()->executeScript("$js.insertHtml('$text');");
50
  }
51
52
  /**
53
   * Asserts that a CKEditor's content contains a snippet of text.
54
   *
55
   * @param string $text
56
   *   The text (or HTML) snippet to look for.
57
   * @param string $id
58
   *   (optional) The editor instance ID.
59
   *
60
   * @throws ExpectationException
61
   *   If the editor does not contain the specified text.
62
   *
63
   * @Then CKEditor should contain :text
64
   * @Then CKEditor :id should contain :text
65
   */
66 View Code Duplication
  public function assertEditorContains($text, $id = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    $position = strpos($this->getContent($id), $text);
68
69
    if ($position == FALSE) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $position of type integer to the boolean FALSE. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
70
      throw new ExpectationException(
71
        'Expected CKEditor ' . $id . ' to contain "' . $text . '".',
72
        $this->getSession()->getDriver()
73
      );
74
    }
75
  }
76
77
  /**
78
   * Assert that a CKEditor's content matches a regular expression.
79
   *
80
   * @param string $expr
81
   *   The regular expression to match.
82
   * @param string $id
83
   *   (optional) The editor instance ID.
84
   *
85
   * @throws ExpectationException
86
   *   If the expression does not match.
87
   *
88
   * @Then CKEditor should match :expression
89
   * @Then CKEditor :id should match :expression
90
   */
91 View Code Duplication
  public function assertEditorMatch($expr, $id = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    $match = preg_match($expr, $this->getContent($id));
93
94
    if ($match == 0) {
95
      throw new ExpectationException(
96
        'Expected CKEditor ' . $id . ' to match "' . $expr . '".',
97
        $this->getSession()->getDriver()
98
      );
99
    }
100
  }
101
102
  /**
103
   * Gets the content of a CKEditor instance.
104
   *
105
   * @param string $id
106
   *   (optional) The editor instance ID.
107
   *
108
   * @return string
109
   *   The HTML content of the editor.
110
   */
111
  protected function getContent($id = NULL) {
112
    $js = $this->assertEditor($id);
113
    return $this->getSession()->evaluateScript("$js.getData()");
114
  }
115
116
  /**
117
   * Executes a CKEditor command.
118
   *
119
   * @param string $command
120
   *   The command ID, as known to CKEditor's API.
121
   * @param string $id
122
   *   (optional) The editor instance ID.
123
   * @param mixed $data
124
   *   Additional data to pass to the executed command.
125
   *
126
   * @throws ExpectationException
127
   *   If the command cannot be executed (i.e., returns a falsy value).
128
   *
129
   * @When I execute the :command command in CKEditor
130
   * @When I execute the :command command in CKEditor :id
131
   */
132
  public function execute($command, $id = NULL, $data = NULL) {
133
    $js = $this->assertEditor($id);
134
135
    $session = $this->getSession();
136
137
    $return = Json::decode($session->evaluateScript("$js.execCommand('$command', " . Json::encode($data) . ')'));
138
139
    if (empty($return)) {
140
      throw new ExpectationException(
141
        'CKEditor command ' . $command . ' returned ' . var_export($return, TRUE) . ', expected truthy.',
142
        $session->getDriver()
143
      );
144
    }
145
  }
146
147
  /**
148
   * Returns the first available CKEditor instance ID.
149
   *
150
   * @return string|false
151
   *   The first CKEditor instance ID, or FALSE if there are no instances.
152
   */
153
  protected function getDefault() {
154
    $keys = $this->getKeys();
155
    return reset($keys);
156
  }
157
158
  /**
159
   * Returns all CKEditor instance IDs.
160
   *
161
   * @return string[]
162
   *   The CKEditor instance IDs.
163
   */
164
  protected function getKeys() {
165
    $keys = $this
166
      ->getSession()
167
      ->evaluateScript('Object.keys(CKEDITOR.instances).join(",")');
168
169
    return explode(',', $keys);
170
  }
171
172
}
173