annotate core/modules/simpletest/tests/src/Unit/WebTestBaseTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\simpletest\Unit;
Chris@0 4
Chris@0 5 use Drupal\Tests\UnitTestCase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * @requires extension curl
Chris@0 9 * @coversDefaultClass \Drupal\simpletest\WebTestBase
Chris@0 10 * @group simpletest
Chris@18 11 * @group WebTestBase
Chris@0 12 */
Chris@0 13 class WebTestBaseTest extends UnitTestCase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Provides data for testing the assertFieldByName() helper.
Chris@0 17 *
Chris@0 18 * @return array
Chris@0 19 * An array of values passed to the test method.
Chris@0 20 */
Chris@0 21 public function providerAssertFieldByName() {
Chris@0 22 $data = [];
Chris@0 23 $data[] = ['select_2nd_selected', 'test', '1', FALSE];
Chris@0 24 $data[] = ['select_2nd_selected', 'test', '2', TRUE];
Chris@0 25 $data[] = ['select_none_selected', 'test', '', FALSE];
Chris@0 26 $data[] = ['select_none_selected', 'test', '1', TRUE];
Chris@0 27 $data[] = ['select_none_selected', 'test', NULL, TRUE];
Chris@0 28
Chris@0 29 return $data;
Chris@0 30 }
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Tests the assertFieldByName() helper.
Chris@0 34 *
Chris@0 35 * @param string $filename
Chris@0 36 * Name of file containing the output to test.
Chris@0 37 * @param string $name
Chris@0 38 * Name of field to assert.
Chris@0 39 * @param string $value
Chris@0 40 * Value of the field to assert.
Chris@0 41 * @param bool $expected
Chris@0 42 * The expected result of the assert.
Chris@0 43 *
Chris@0 44 * @see \Drupal\simpletest\WebTestBase::assertFieldByName()
Chris@0 45 *
Chris@0 46 * @dataProvider providerAssertFieldByName
Chris@0 47 * @covers ::assertFieldByName
Chris@0 48 */
Chris@0 49 public function testAssertFieldByName($filename, $name, $value, $expected) {
Chris@0 50 $content = file_get_contents(__DIR__ . '/../../fixtures/' . $filename . '.html');
Chris@0 51
Chris@0 52 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
Chris@0 53 ->disableOriginalConstructor()
Chris@0 54 ->setMethods(['getRawContent', 'assertTrue', 'pass'])
Chris@0 55 ->getMock();
Chris@0 56
Chris@0 57 $web_test->expects($this->any())
Chris@0 58 ->method('getRawContent')
Chris@0 59 ->will($this->returnValue($content));
Chris@0 60
Chris@0 61 $web_test->expects($this->once())
Chris@0 62 ->method('assertTrue')
Chris@0 63 ->with($this->identicalTo($expected),
Chris@0 64 $this->identicalTo('message'),
Chris@0 65 $this->identicalTo('Browser'));
Chris@0 66
Chris@0 67 $test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName');
Chris@0 68 $test_method->setAccessible(TRUE);
Chris@0 69 $test_method->invokeArgs($web_test, [$name, $value, 'message']);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Data provider for testClickLink().
Chris@0 74 *
Chris@0 75 * In the test method, we mock drupalGet() to return a known string:
Chris@0 76 * 'This Text Returned By drupalGet()'. Since clickLink() can only return
Chris@0 77 * either the value of drupalGet() or FALSE, our expected return value is the
Chris@0 78 * same as this mocked return value when we expect a link to be found.
Chris@0 79 *
Chris@0 80 * @see https://www.drupal.org/node/1452896
Chris@0 81 *
Chris@0 82 * @return array
Chris@0 83 * Array of arrays of test data. Test data is structured as follows:
Chris@0 84 * - Expected return value of clickLink().
Chris@0 85 * - Parameter $label to clickLink().
Chris@0 86 * - Parameter $index to clickLink().
Chris@0 87 * - Test data to be returned by mocked xpath(). Return an empty array here
Chris@0 88 * to mock no link found on the page.
Chris@0 89 */
Chris@0 90 public function providerTestClickLink() {
Chris@0 91 return [
Chris@0 92 // Test for a non-existent label.
Chris@0 93 [
Chris@0 94 FALSE,
Chris@0 95 'does_not_exist',
Chris@0 96 0,
Chris@0 97 [],
Chris@0 98 ],
Chris@0 99 // Test for an existing label.
Chris@0 100 [
Chris@0 101 'This Text Returned By drupalGet()',
Chris@0 102 'exists',
Chris@0 103 0,
Chris@0 104 [0 => ['href' => 'this_is_a_url']],
Chris@0 105 ],
Chris@0 106 // Test for an existing label that isn't the first one.
Chris@0 107 [
Chris@0 108 'This Text Returned By drupalGet()',
Chris@0 109 'exists',
Chris@0 110 1,
Chris@0 111 [
Chris@0 112 0 => ['href' => 'this_is_a_url'],
Chris@0 113 1 => ['href' => 'this_is_another_url'],
Chris@0 114 ],
Chris@0 115 ],
Chris@0 116 ];
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Test WebTestBase::clickLink().
Chris@0 121 *
Chris@0 122 * @param mixed $expected
Chris@0 123 * Expected return value of clickLink().
Chris@0 124 * @param string $label
Chris@0 125 * Parameter $label to clickLink().
Chris@0 126 * @param int $index
Chris@0 127 * Parameter $index to clickLink().
Chris@0 128 * @param array $xpath_data
Chris@0 129 * Test data to be returned by mocked xpath().
Chris@0 130 *
Chris@0 131 * @dataProvider providerTestClickLink
Chris@0 132 * @covers ::clickLink
Chris@0 133 */
Chris@0 134 public function testClickLink($expected, $label, $index, $xpath_data) {
Chris@0 135 // Mock a WebTestBase object and some of its methods.
Chris@0 136 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
Chris@0 137 ->disableOriginalConstructor()
Chris@0 138 ->setMethods([
Chris@0 139 'pass',
Chris@0 140 'fail',
Chris@0 141 'getUrl',
Chris@0 142 'xpath',
Chris@0 143 'drupalGet',
Chris@0 144 'getAbsoluteUrl',
Chris@0 145 ])
Chris@0 146 ->getMock();
Chris@0 147
Chris@0 148 // Mocked getUrl() is only used for reporting so we just return a string.
Chris@0 149 $web_test->expects($this->any())
Chris@0 150 ->method('getUrl')
Chris@0 151 ->will($this->returnValue('url_before'));
Chris@0 152
Chris@0 153 // Mocked xpath() should return our test data.
Chris@0 154 $web_test->expects($this->any())
Chris@0 155 ->method('xpath')
Chris@0 156 ->will($this->returnValue($xpath_data));
Chris@0 157
Chris@0 158 if ($expected === FALSE) {
Chris@0 159 // If link does not exist clickLink() will not try to do a drupalGet() or
Chris@0 160 // a getAbsoluteUrl()
Chris@0 161 $web_test->expects($this->never())
Chris@0 162 ->method('drupalGet');
Chris@0 163 $web_test->expects($this->never())
Chris@0 164 ->method('getAbsoluteUrl');
Chris@0 165 // The test should fail and not pass.
Chris@0 166 $web_test->expects($this->never())
Chris@0 167 ->method('pass');
Chris@0 168 $web_test->expects($this->once())
Chris@0 169 ->method('fail')
Chris@0 170 ->will($this->returnValue(NULL));
Chris@0 171 }
Chris@0 172 else {
Chris@0 173 // Mocked getAbsoluteUrl() should return whatever comes in.
Chris@0 174 $web_test->expects($this->once())
Chris@0 175 ->method('getAbsoluteUrl')
Chris@0 176 ->with($xpath_data[$index]['href'])
Chris@0 177 ->will($this->returnArgument(0));
Chris@0 178 // We're only testing clickLink(), so drupalGet() always returns a string.
Chris@0 179 $web_test->expects($this->once())
Chris@0 180 ->method('drupalGet')
Chris@0 181 ->with($xpath_data[$index]['href'])
Chris@0 182 ->will($this->returnValue('This Text Returned By drupalGet()'));
Chris@0 183 // The test should pass and not fail.
Chris@0 184 $web_test->expects($this->never())
Chris@0 185 ->method('fail');
Chris@0 186 $web_test->expects($this->once())
Chris@0 187 ->method('pass')
Chris@0 188 ->will($this->returnValue(NULL));
Chris@0 189 }
Chris@0 190
Chris@0 191 // Set the clickLink() method to public so we can test it.
Chris@0 192 $clicklink_method = new \ReflectionMethod($web_test, 'clickLink');
Chris@0 193 $clicklink_method->setAccessible(TRUE);
Chris@0 194
Chris@0 195 $this->assertSame($expected, $clicklink_method->invoke($web_test, $label, $index));
Chris@0 196 }
Chris@0 197
Chris@0 198 /**
Chris@0 199 * @dataProvider providerTestGetAbsoluteUrl
Chris@0 200 */
Chris@0 201 public function testGetAbsoluteUrl($href, $expected_absolute_path) {
Chris@0 202 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
Chris@0 203 ->disableOriginalConstructor()
Chris@0 204 ->setMethods(['getUrl'])
Chris@0 205 ->getMock();
Chris@0 206
Chris@0 207 $web_test->expects($this->any())
Chris@0 208 ->method('getUrl')
Chris@0 209 ->willReturn('http://example.com/drupal/current-path?foo=baz');
Chris@0 210
Chris@0 211 $GLOBALS['base_url'] = 'http://example.com';
Chris@0 212 $GLOBALS['base_path'] = 'drupal';
Chris@0 213
Chris@0 214 $get_absolute_url_method = new \ReflectionMethod($web_test, 'getAbsoluteUrl');
Chris@0 215 $get_absolute_url_method->setAccessible(TRUE);
Chris@0 216
Chris@0 217 $this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href));
Chris@0 218 unset($GLOBALS['base_url'], $GLOBALS['base_path']);
Chris@0 219 }
Chris@0 220
Chris@0 221 /**
Chris@0 222 * Provides test data for testGetAbsoluteUrl.
Chris@0 223 *
Chris@0 224 * @return array
Chris@0 225 */
Chris@0 226 public function providerTestGetAbsoluteUrl() {
Chris@0 227 $data = [];
Chris@0 228 $data['host'] = ['http://example.com/drupal/test-example', 'http://example.com/drupal/test-example'];
Chris@0 229 $data['path'] = ['/drupal/test-example', 'http://example.com/drupal/test-example'];
Chris@0 230 $data['path-with-query'] = ['/drupal/test-example?foo=bar', 'http://example.com/drupal/test-example?foo=bar'];
Chris@0 231 $data['just-query'] = ['?foo=bar', 'http://example.com/drupal/current-path?foo=bar'];
Chris@0 232
Chris@0 233 return $data;
Chris@0 234 }
Chris@0 235
Chris@0 236 }