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

UtilityContext::assertFieldNotExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Acquia\DFExtension\Context;
4
5
use Behat\Mink\Element\ElementInterface;
6
use Behat\Mink\Exception\ExpectationException;
7
use Drupal\Component\Utility\Random;
8
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
9
10
/**
11
 * A context with miscellaneous helpers.
12
 */
13
class UtilityContext extends DrupalSubContextBase {
14
15
  /**
16
   * Asserts that a form field is not present.
17
   *
18
   * @param string $field
19
   *   The field locator.
20
   *
21
   * @Then I should not see a :field field
22
   */
23
  public function assertFieldNotExists($field) {
24
    $this->assertSession()->fieldNotExists($field);
25
  }
26
27
  /**
28
   * Asserts that a certain number of elements match a CSS selector.
29
   *
30
   * @param string $selector
31
   *   The selector.
32
   * @param int $count
33
   *   The number of elements expected to match the selector.
34
   *
35
   * @throws ExpectationException
36
   *   If the number of elements that match the selector is not the expected
37
   *   number.
38
   *
39
   * @Then :count element(s) should match :selector
40
   */
41 View Code Duplication
  public function assertSelectorMatch($selector, $count) {
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...
42
    $session = $this->getSession();
43
44
    $result = count($session->getPage()->findAll('css', $selector));
45
46
    if ($result != $count) {
47
      throw new ExpectationException(
48
        '"' . $selector . '" matched ' . $result . ' element(s), expected ' . $count . '.',
49
        $session->getDriver()
50
      );
51
    }
52
  }
53
54
  /**
55
   * Asserts that a minimum number of elements match a CSS selector.
56
   *
57
   * @param string $selector
58
   *   The selector.
59
   * @param int $count
60
   *   The number of elements expected to match the selector.
61
   *
62
   * @throws ExpectationException
63
   *   If the number of elements that match the selector is less than expected.
64
   *
65
   * @Then at least :count element(s) should match :selector
66
   */
67 View Code Duplication
  public function assertSelectorMatchAtLeast($selector, $count) {
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...
68
    $session = $this->getSession();
69
70
    $result = count($session->getPage()->findAll('css', $selector));
71
72
    if ($result < $count) {
73
      throw new ExpectationException(
74
        '"' . $selector . '" matched ' . $result . ' element(s), expected at least ' . $count . '.',
75
        $session->getDriver()
76
      );
77
    }
78
  }
79
80
  /**
81
   * Asserts that no elements match a CSS selector.
82
   *
83
   * @param string $selector
84
   *   The selector.
85
   *
86
   * @Then no elements should match :selector
87
   * @Then nothing should match :selector
88
   */
89
  public function assertSelectorMatchNothing($selector) {
90
    $this->assertSelectorMatch($selector, 0);
91
  }
92
93
  /**
94
   * Asserts than an element is empty.
95
   *
96
   * @param string $selector
97
   *   The element's CSS selector.
98
   *
99
   * @throws ExpectationException
100
   *   If the element has any HTML content.
101
   *
102
   * @Then the :selector element(s) should be empty
103
   */
104
  public function assertElementIsEmpty($selector) {
105
    $content = $this->assertSession()->elementExists('css', $selector)->getHtml();
106
107
    if (trim($content)) {
108
      throw new ExpectationException(
109
        'Expected ' . $selector . ' to be empty but it is not.',
110
        $this->getSession()->getDriver()
111
      );
112
    }
113
  }
114
115
  /**
116
   * Clears a field.
117
   *
118
   * @param string $field
119
   *   The field to clear.
120
   * @param ElementInterface $container
121
   *   (optional) The containing element.
122
   *
123
   * @When I clear :field
124
   */
125
  public function clearField($field, ElementInterface $container = NULL) {
126
    $this->assertSession()->fieldExists($field, $container)->setValue(FALSE);
0 ignored issues
show
Bug introduced by
It seems like $container defined by parameter $container on line 125 can also be of type object<Behat\Mink\Element\ElementInterface>; however, Behat\Mink\WebAssert::fieldExists() does only seem to accept null|object<Behat\Mink\E...ent\TraversableElement>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127
  }
128
129
  /**
130
   * Finds a collapsible details element by its summary text.
131
   *
132
   * @param string $summary_text
133
   *   The summary text.
134
   *
135
   * @return \Behat\Mink\Element\NodeElement|null
136
   *   The details element, or NULL if it was not found.
137
   */
138
  public function findCollapsible($summary_text) {
139
    /** @var \Behat\Mink\Element\NodeElement[] $elements */
140
    $elements = $this
141
      ->getSession()
142
      ->getPage()
143
      ->findAll('css', 'details > summary');
144
145
    foreach ($elements as $element) {
146
      if ($element->getText() == $summary_text) {
147
        return $element->getParent();
148
      }
149
    }
150
  }
151
152
  /**
153
   * Generates random image media assets.
154
   *
155
   * @param int $count
156
   *   (optional) How many to generate. Defaults to 1.
157
   *
158
   * @Given a random image
159
   * @Given :count random images
160
   */
161
  public function generateRandomImages($count = 1) {
162
    $random = new Random();
163
    $entity_type_manager = \Drupal::entityTypeManager();
164
165
    for ($i = 0; $i < $count; $i++) {
166
      $uri = $random->image(uniqid('public://random_') . '.png', '240x240', '640x480');
0 ignored issues
show
Bug introduced by
The method image() does not seem to exist on object<Drupal\Component\Utility\Random>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
168
      /** @var \Drupal\file\FileInterface $file */
169
      $file = $entity_type_manager->getStorage('file')->create(['uri' => $uri]);
170
      $file->setMimeType('image/png');
171
      $file->setTemporary();
172
      $file->save();
173
174
      $media = $entity_type_manager->getStorage('media')->create([
175
        'bundle' => 'image',
176
        'name' => $random->name(32),
177
        'status' => TRUE,
178
        'image' => $file->id(),
179
        'field_media_in_library' => TRUE,
180
      ]);
181
      $media->save();
182
    }
183
  }
184
185
}
186