Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Assert / AssertLegacyTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Assert;
4
5 use Behat\Mink\Element\DocumentElement;
6 use Behat\Mink\Element\NodeElement;
7 use Behat\Mink\Session;
8 use Drupal\Component\Render\MarkupInterface;
9 use Drupal\FunctionalTests\AssertLegacyTrait;
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\Tests\WebAssert;
12 use PHPUnit_Framework_ExpectationFailedException;
13
14 /**
15  * @coversDefaultClass \Drupal\FunctionalTests\AssertLegacyTrait
16  * @group Assert
17  */
18 class AssertLegacyTraitTest extends UnitTestCase {
19
20   use AssertLegacyTrait;
21
22   /**
23    * The mocked Mink session object used for testing.
24    *
25    * @var \Behat\Mink\Session|\Prophecy\Prophecy\ObjectProphecy
26    */
27   protected $session;
28
29   /**
30    * The mocked page element used for testing.
31    *
32    * @var Behat\Mink\Element\DocumentElement|\Prophecy\Prophecy\ObjectProphecy
33    */
34   protected $page;
35
36   /**
37    * The mocked web assert class.
38    *
39    * @var \Drupal\Tests\WebAssert|\Prophecy\Prophecy\ObjectProphecy
40    */
41   protected $webAssert;
42
43   /**
44    * {@inheritdoc}
45    */
46   public function setUp() {
47     parent::setUp();
48
49     $this->page = $this->prophesize(DocumentElement::class);
50     $this->session = $this->prophesize(Session::class);
51     $this->session->getPage()->willReturn($this->page->reveal());
52     $this->webAssert = $this->prophesize(WebAssert::class);
53   }
54
55   /**
56    * @covers ::assertUniqueText
57    */
58   public function testAssertUniqueText() {
59     $this->page->getText()->willReturn('foo bar bar');
60     $this->assertUniqueText('foo');
61   }
62
63   /**
64    * @covers ::assertUniqueText
65    */
66   public function testAssertUniqueTextFail() {
67     $this->page->getText()->willReturn('foo bar bar');
68     $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
69     $this->assertUniqueText('bar');
70   }
71
72   /**
73    * @covers ::assertUniqueText
74    */
75   public function testAssertUniqueTextUnknown() {
76     $this->page->getText()->willReturn('foo bar bar');
77     $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
78     $this->assertUniqueText('alice');
79   }
80
81   /**
82    * @covers ::assertUniqueText
83    */
84   public function testAssertUniqueTextMarkup() {
85     $this->page->getText()->willReturn('foo bar bar');
86     $markupObject = $this->prophesize(MarkupInterface::class);
87     $markupObject->__toString()->willReturn('foo');
88     $this->assertUniqueText($markupObject->reveal());
89   }
90
91   /**
92    * @covers ::assertNoUniqueText
93    */
94   public function testAssertNoUniqueText() {
95     $this->page->getText()->willReturn('foo bar bar');
96     $this->assertNoUniqueText('bar');
97   }
98
99   /**
100    * @covers ::assertNoUniqueText
101    */
102   public function testAssertNoUniqueTextFail() {
103     $this->page->getText()->willReturn('foo bar bar');
104     $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
105     $this->assertNoUniqueText('foo');
106   }
107
108   /**
109    * @covers ::assertNoUniqueText
110    */
111   public function testAssertNoUniqueTextUnknown() {
112     $this->page->getText()->willReturn('foo bar bar');
113     $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
114     $this->assertNoUniqueText('alice');
115   }
116
117   /**
118    * @covers ::assertNoUniqueText
119    */
120   public function testAssertNoUniqueTextMarkup() {
121     $this->page->getText()->willReturn('foo bar bar');
122     $markupObject = $this->prophesize(MarkupInterface::class);
123     $markupObject->__toString()->willReturn('bar');
124     $this->assertNoUniqueText($markupObject->reveal());
125   }
126
127   /**
128    * @covers ::assertOptionSelected
129    */
130   public function testAssertOptionSelected() {
131     $option_field = $this->prophesize(NodeElement::class);
132     $option_field->hasAttribute('selected')->willReturn(TRUE);
133
134     $this->webAssert
135       ->optionExists('myselect', 'two')
136       ->willReturn($option_field->reveal());
137
138     $this->assertOptionSelected('myselect', 'two');
139   }
140
141   /**
142    * @covers ::assertOptionSelected
143    */
144   public function testAssertOptionSelectedFail() {
145     $option_field = $this->prophesize(NodeElement::class);
146     $option_field->hasAttribute('selected')->willReturn(FALSE);
147
148     $this->webAssert
149       ->optionExists('myselect', 'two')
150       ->willReturn($option_field->reveal());
151
152     $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
153     $this->assertOptionSelected('myselect', 'two');
154   }
155
156   /**
157    * @covers ::assertNoPattern
158    */
159   public function testAssertNoPattern() {
160     $this->webAssert
161       ->responseNotMatches('/.*foo$/')
162       ->shouldBeCalled();
163
164     $this->assertNoPattern('/.*foo$/');
165   }
166
167   /**
168    * Returns a mocked behat session object.
169    *
170    * @return \Behat\Mink\Session
171    *   The mocked session.
172    */
173   protected function getSession() {
174     return $this->session->reveal();
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function assertSession($name = NULL) {
181     return $this->webAssert->reveal();
182   }
183
184 }