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