CKEditorContext::iFillInTheRichTextEditorWith()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\MinkExtension\Context\RawMinkContext;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NuvoleWeb\Drupal\DrupalE...\Context\RawMinkContext.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * Class CKEditorContext.
9
 *
10
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
11
 */
12
class CKEditorContext extends RawMinkContext {
13
14
  /**
15
   * Fill CKEditor with given value.
16
   *
17
   * @Given I fill in the rich text editor :label with :text
18
   */
19
  public function iFillInTheRichTextEditorWith($label, $text) {
20
    /** @var \Behat\Mink\Element\NodeElement $field */
21
    $field = $this->getSession()->getPage()->findField($label);
22
23
    if (NULL === $field) {
24
      throw new \Exception(sprintf('Field "%s" not found.', $label));
25
    }
26
27
    $args_as_js_object = json_encode([
28
      'ckeditor_instance_id' => $field->getAttribute('id'),
29
      'value' => $text,
30
    ]);
31
32
    $this->getSession()->executeScript(
33
      "args = {$args_as_js_object};" .
34
      "CKEDITOR.instances[args.ckeditor_instance_id].setData(args.value);"
35
    );
36
  }
37
38
}
39