annotate core/modules/simpletest/tests/src/Unit/WebTestBaseTest.php @ 0:c75dbcec494b

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