DrupalContext::assertAccessDenied()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Gherkin\Node\TableNode;
6
7
/**
8
 * Extends Drupal Extension DrupalContext class.
9
 *
10
 * Load this context instead of Drupal\DrupalExtension\Context\DrupalContext.
11
 *
12
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
13
 */
14
class DrupalContext extends RawDrupalContext {
15
16
  /**
17
   * Assert access denied page.
18
   *
19
   * @Then I should get an access denied error
20
   */
21
  public function assertAccessDenied() {
22
    $this->assertSession()->statusCodeEquals(403);
23
  }
24
25
  /**
26
   * Pause execution for given number of seconds.
27
   *
28
   * @Then I wait :seconds seconds
29
   */
30
  public function iWaitSeconds($seconds) {
31
    sleep((int) $seconds);
32
  }
33
34
  /**
35
   * Creates content by filling specified form fields via the UI.
36
   *
37
   * Use as follow:
38
   *
39
   *  | Title    | Author     | Label | of the field      |
40
   *  | My title | Joe Editor | 1     | 2014-10-17 8:00am |
41
   *  | ...      | ...        | ...   | ...               |
42
   *
43
   * @Given I create :type content:
44
   */
45
  public function manuallyCreateNodes($type, TableNode $nodesTable) {
46
    $type = $this->getCore()->convertLabelToNodeTypeId($type);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\Cores\CoreInterface as the method convertLabelToNodeTypeId() does only exist in the following implementations of said interface: NuvoleWeb\Drupal\Driver\Cores\Drupal8.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
47
48
    foreach ($nodesTable->getHash() as $nodeHash) {
49
      $this->getSession()->visit($this->locatePath("/node/add/$type"));
50
      $element = $this->getSession()->getPage();
51
      // Fill in the form.
52
      foreach ($nodeHash as $field => $value) {
53
        $element->fillField($field, $value);
54
      }
55
      $submit = $element->findButton($this->getDrupalText('node_submit_label'));
56
      if (empty($submit)) {
57
        throw new \Exception(sprintf("No submit button at %s", $this->getSession()->getCurrentUrl()));
58
      }
59
      // Submit the form.
60
      $submit->click();
61
    }
62
63
  }
64
65
  /**
66
   * Assert string in HTTP response header.
67
   *
68
   * @Then I should see in the header :header::value
69
   */
70
  public function iShouldSeeInTheHeader($header, $value) {
71
    $headers = $this->getSession()->getResponseHeaders();
72
    if ($headers[$header] != $value) {
73
      throw new \Exception(sprintf("Did not see %s with value %s.", $header, $value));
74
    }
75
  }
76
77
  /**
78
   * Invalidate cache tags.
79
   *
80
   * Usage:
81
   *
82
   * Given I invalidate the following cache tags:
83
   *   | tag_one |
84
   *   | tag_two |
85
   *
86
   * @Given I invalidate the following cache tags:
87
   */
88
  public function invalidateCacheTags(TableNode $table) {
89
    $tags = [];
90
    foreach ($table->getRows() as $row) {
91
      $tags[] = $row[0];
92
    }
93
    $this->getCore()->invalidateCacheTags($tags);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\Cores\CoreInterface as the method invalidateCacheTags() does only exist in the following implementations of said interface: NuvoleWeb\Drupal\Driver\Cores\Drupal8.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
94
  }
95
96
}
97