Completed
Push — 1.0.x ( 235ffe...0a17d9 )
by Antonio
08:29
created

Generic::iShouldNotSeeTheField()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Traits;
4
5
use Behat\Mink\Element\NodeElement;
6
use Behat\Mink\Exception\ExpectationException;
7
use Behat\Mink\Exception\UnsupportedDriverActionException;
8
9
/**
10
 * Trait Generic.
11
 *
12
 * @package Nuvole\Drupal\Behat\Traits
13
 */
14
trait Generic {
15
16
  /**
17
   * Checks that the given element is of the given type.
18
   *
19
   * @param NodeElement $element
20
   *   The element to check.
21
   * @param string $type
22
   *   The expected type.
23
   *
24
   * @throws ExpectationException
25
   *   Thrown when the given element is not of the expected type.
26
   */
27
  public function assertElementType(NodeElement $element, $type) {
28
    if ($element->getTagName() !== $type) {
29
      throw new ExpectationException("The element is not a '$type'' field.", $this->getSession());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
    }
31
  }
32
33
  /**
34
   * Assert presence of given field on the page.
35
   *
36
   * @Then I should see the field :field
37
   */
38 View Code Duplication
  public function iShouldSeeTheField($field) {
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...
39
    $element = $this->getSession()->getPage();
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    $result = $element->findField($field);
41
    try {
42
      if ($result && !$result->isVisible()) {
43
        throw new \Exception(sprintf("No field '%s' on the page %s", $field, $this->getSession()->getCurrentUrl()));
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
      }
45
    }
46
    catch (UnsupportedDriverActionException $e) {
47
      // We catch the UnsupportedDriverActionException exception in case
48
      // this step is not being performed by a driver that supports javascript.
49
      // All other exceptions are valid.
50
    }
51
    if (empty($result)) {
52
      throw new \Exception(sprintf("No field '%s' on the page %s", $field, $this->getSession()->getCurrentUrl()));
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
    }
54
  }
55
56
  /**
57
   * Assert absence of given field.
58
   *
59
   * @Then I should not see the field :field
60
   */
61 View Code Duplication
  public function iShouldNotSeeTheField($field) {
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...
62
    $element = $this->getSession()->getPage();
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
    $result = $element->findField($field);
64
    try {
65
      if ($result && $result->isVisible()) {
66
        throw new \Exception(sprintf("The field '%s' was present on the page %s and was not supposed to be", $field, $this->getSession()->getCurrentUrl()));
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
      }
68
    }
69
    catch (UnsupportedDriverActionException $e) {
70
      // We catch the UnsupportedDriverActionException exception in case
71
      // this step is not being performed by a driver that supports javascript.
72
      // All other exceptions are valid.
73
      if ($result) {
74
        throw new \Exception(sprintf("The field '%s' was present on the page %s and was not supposed to be", $field, $this->getSession()->getCurrentUrl()));
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
      }
76
    }
77
  }
78
79
  /**
80
   * Visit taxonomy term page given its type and name.
81
   *
82
   * @Given I am visiting the :type term :title
83
   * @Given I visit the :type term :title
84
   */
85
  public function iAmViewingTheTerm($type, $title) {
86
    $this->visitTermPage('view', $type, $title);
87
  }
88
89
  /**
90
   * Visit taxonomy term edit page given its type and name.
91
   *
92
   * @Given I am editing the :type term :title
93
   * @Given I edit the :type term :title
94
   */
95
  public function iAmEditingTheTerm($type, $title) {
96
    $this->visitTermPage('edit', $type, $title);
97
  }
98
99
  /**
100
   * Provides a common step definition callback for node pages.
101
   *
102
   * @param string $op
103
   *   The operation being performed: 'view', 'edit', 'delete'.
104
   * @param string $type
105
   *   The node type either as id or as label.
106
   * @param string $title
107
   *   The node title.
108
   *
109
   * @throws ExpectationException
110
   *   When the node does not exist.
111
   */
112
  protected function visitTermPage($op, $type, $title) {
113
    $type = $this->convertLabelToTermTypeId($type);
0 ignored issues
show
Bug introduced by
It seems like convertLabelToTermTypeId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114
    $result = \Drupal::entityQuery('taxonomy_term')
115
      ->condition('vid', $type)
116
      ->condition('name', $title)
117
      ->execute();
118
119
    if (!empty($result)) {
120
      $tid = array_shift($result);
121
      $path = [
122
        'view' => "taxonomy/term/$tid",
123
        'edit' => "taxonomy/term/$tid/edit",
124
        'delete' => "taxonomy/term/$tid/delete",
125
      ];
126
      $this->visitPath($path[$op]);
0 ignored issues
show
Bug introduced by
It seems like visitPath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
127
    }
128
    else {
129
      throw new ExpectationException("No term with vocabulary '$type' and title '$title' has been found.", $this->getSession());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
130
    }
131
  }
132
133
  /**
134
   * Assert first element precedes second one.
135
   *
136
   * @Then :first should precede :second
137
   */
138
  public function shouldPrecede($first, $second) {
139
    $page = $this->getSession()->getPage()->getText();
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
140
    $pos1 = strpos($page, $first);
141
    if ($pos1 === FALSE) {
142
      throw new ExpectationException("Text not found: '$first'.", $this->getSession());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
143
    }
144
    $pos2 = strpos($page, $second);
145
    if ($pos2 === FALSE) {
146
      throw new ExpectationException("Text not found: '$second'.", $this->getSession());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
147
    }
148
    if ($pos2 < $pos1) {
149
      throw new \Exception("Text '$first' does not precede text '$second'.");
150
    }
151
  }
152
153
}
154