BrowserTestBaseTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Behat\Mink\Exception\ExpectationException;
  4. use Drupal\Component\Utility\Html;
  5. use Drupal\Core\Url;
  6. use Drupal\Tests\BrowserTestBase;
  7. use Drupal\Tests\Traits\Core\CronRunTrait;
  8. /**
  9. * Tests BrowserTestBase functionality.
  10. *
  11. * @group browsertestbase
  12. */
  13. class BrowserTestBaseTest extends BrowserTestBase {
  14. use CronRunTrait;
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['test_page_test', 'form_test', 'system_test'];
  21. /**
  22. * Tests basic page test.
  23. */
  24. public function testGoTo() {
  25. $account = $this->drupalCreateUser();
  26. $this->drupalLogin($account);
  27. // Visit a Drupal page that requires login.
  28. $this->drupalGet('test-page');
  29. $this->assertSession()->statusCodeEquals(200);
  30. // Test page contains some text.
  31. $this->assertSession()->pageTextContains('Test page text.');
  32. // Check that returned plain text is correct.
  33. $text = $this->getTextContent();
  34. $this->assertContains('Test page text.', $text);
  35. $this->assertNotContains('</html>', $text);
  36. // Response includes cache tags that we can assert.
  37. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
  38. // Test that we can read the JS settings.
  39. $js_settings = $this->getDrupalSettings();
  40. $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
  41. // Test drupalGet with a url object.
  42. $url = Url::fromRoute('test_page_test.render_title');
  43. $this->drupalGet($url);
  44. $this->assertSession()->statusCodeEquals(200);
  45. // Test page contains some text.
  46. $this->assertSession()->pageTextContains('Hello Drupal');
  47. // Test that setting headers with drupalGet() works.
  48. $this->drupalGet('system-test/header', [], [
  49. 'Test-Header' => 'header value',
  50. ]);
  51. $returned_header = $this->getSession()->getResponseHeader('Test-Header');
  52. $this->assertSame('header value', $returned_header);
  53. }
  54. /**
  55. * Tests basic form functionality.
  56. */
  57. public function testForm() {
  58. // Ensure the proper response code for a _form route.
  59. $this->drupalGet('form-test/object-builder');
  60. $this->assertSession()->statusCodeEquals(200);
  61. // Ensure the form and text field exist.
  62. $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
  63. $this->assertSession()->fieldExists('bananas');
  64. // Check that the hidden field exists and has a specific value.
  65. $this->assertSession()->hiddenFieldExists('strawberry');
  66. $this->assertSession()->hiddenFieldExists('red');
  67. $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
  68. $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
  69. $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
  70. // Check that a hidden field does not exist.
  71. $this->assertSession()->hiddenFieldNotExists('bananas');
  72. $this->assertSession()->hiddenFieldNotExists('pineapple');
  73. $edit = ['bananas' => 'green'];
  74. $this->submitForm($edit, 'Save', 'form-test-form-test-object');
  75. $config_factory = $this->container->get('config.factory');
  76. $value = $config_factory->get('form_test.object')->get('bananas');
  77. $this->assertSame('green', $value);
  78. // Test drupalPostForm().
  79. $edit = ['bananas' => 'red'];
  80. $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
  81. $value = $config_factory->get('form_test.object')->get('bananas');
  82. $this->assertSame('red', $value);
  83. $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
  84. $value = $config_factory->get('form_test.object')->get('bananas');
  85. $this->assertSame('', $value);
  86. }
  87. /**
  88. * Tests clickLink() functionality.
  89. */
  90. public function testClickLink() {
  91. $this->drupalGet('test-page');
  92. $this->clickLink('Visually identical test links');
  93. $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
  94. $this->drupalGet('test-page');
  95. $this->clickLink('Visually identical test links', 0);
  96. $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
  97. $this->drupalGet('test-page');
  98. $this->clickLink('Visually identical test links', 1);
  99. $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
  100. }
  101. public function testError() {
  102. $this->setExpectedException('\Exception', 'User notice: foo');
  103. $this->drupalGet('test-error');
  104. }
  105. /**
  106. * Tests linkExists() with pipe character (|) in locator.
  107. *
  108. * @see \Drupal\Tests\WebAssert::linkExists()
  109. */
  110. public function testPipeCharInLocator() {
  111. $this->drupalGet('test-pipe-char');
  112. $this->assertSession()->linkExists('foo|bar|baz');
  113. }
  114. /**
  115. * Tests legacy text asserts.
  116. */
  117. public function testLegacyTextAsserts() {
  118. $this->drupalGet('test-encoded');
  119. $dangerous = 'Bad html <script>alert(123);</script>';
  120. $sanitized = Html::escape($dangerous);
  121. $this->assertNoText($dangerous);
  122. $this->assertText($sanitized);
  123. // Test getRawContent().
  124. $this->assertSame($this->getSession()->getPage()->getContent(), $this->getRawContent());
  125. }
  126. /**
  127. * Tests legacy field asserts.
  128. */
  129. public function testLegacyFieldAsserts() {
  130. $this->drupalGet('test-field-xpath');
  131. $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
  132. $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
  133. $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
  134. $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
  135. $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
  136. $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
  137. $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
  138. $this->assertNoFieldByXPath('//notexisting');
  139. $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
  140. $this->assertNoFieldById('name');
  141. $this->assertNoFieldById('name', 'not the value');
  142. $this->assertNoFieldById('notexisting');
  143. $this->assertNoFieldById('notexisting', NULL);
  144. // Test that the assertion fails correctly if no value is passed in.
  145. try {
  146. $this->assertNoFieldById('edit-description');
  147. $this->fail('The "description" field, with no value was not found.');
  148. }
  149. catch (ExpectationException $e) {
  150. $this->pass('The "description" field, with no value was found.');
  151. }
  152. // Test that the assertion fails correctly if a NULL value is passed in.
  153. try {
  154. $this->assertNoFieldById('edit-name', NULL);
  155. $this->fail('The "name" field was not found.');
  156. }
  157. catch (ExpectationException $e) {
  158. $this->pass('The "name" field was found.');
  159. }
  160. $this->assertFieldById('edit-name', NULL);
  161. $this->assertFieldById('edit-name', 'Test name');
  162. $this->assertFieldById('edit-description', NULL);
  163. $this->assertFieldById('edit-description');
  164. // Test that the assertion fails correctly if no value is passed in.
  165. try {
  166. $this->assertFieldById('edit-name');
  167. $this->fail('The "edit-name" field with no value was found.');
  168. }
  169. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  170. $this->pass('The "edit-name" field with no value was not found.');
  171. }
  172. // Test that the assertion fails correctly if NULL is passed in.
  173. try {
  174. $this->assertFieldById('name', NULL);
  175. $this->fail('The "name" field was found.');
  176. }
  177. catch (ExpectationException $e) {
  178. $this->pass('The "name" field was not found.');
  179. }
  180. $this->assertNoFieldByName('name');
  181. $this->assertNoFieldByName('name', 'not the value');
  182. $this->assertNoFieldByName('notexisting');
  183. $this->assertNoFieldByName('notexisting', NULL);
  184. // Test that the assertion fails correctly if no value is passed in.
  185. try {
  186. $this->assertNoFieldByName('description');
  187. $this->fail('The "description" field, with no value was not found.');
  188. }
  189. catch (ExpectationException $e) {
  190. $this->pass('The "description" field, with no value was found.');
  191. }
  192. // Test that the assertion fails correctly if a NULL value is passed in.
  193. try {
  194. $this->assertNoFieldByName('name', NULL);
  195. $this->fail('The "name" field was not found.');
  196. }
  197. catch (ExpectationException $e) {
  198. $this->pass('The "name" field was found.');
  199. }
  200. $this->assertOptionByText('options', 'one');
  201. try {
  202. $this->assertOptionByText('options', 'four');
  203. $this->fail('The select option "four" was found.');
  204. }
  205. catch (ExpectationException $e) {
  206. $this->pass($e->getMessage());
  207. }
  208. $this->assertFieldById('edit-save', NULL);
  209. // Test that the assertion fails correctly if the field value is passed in
  210. // rather than the id.
  211. try {
  212. $this->assertFieldById('Save', NULL);
  213. $this->fail('The field with id of "Save" was found.');
  214. }
  215. catch (ExpectationException $e) {
  216. $this->pass($e->getMessage());
  217. }
  218. $this->assertNoFieldById('Save', NULL);
  219. // Test that the assertion fails correctly if the id of an actual field is
  220. // passed in.
  221. try {
  222. $this->assertNoFieldById('edit-save', NULL);
  223. $this->fail('The field with id of "edit-save" was not found.');
  224. }
  225. catch (ExpectationException $e) {
  226. $this->pass($e->getMessage());
  227. }
  228. }
  229. /**
  230. * Tests the ::cronRun() method.
  231. */
  232. public function testCronRun() {
  233. $last_cron_time = \Drupal::state()->get('system.cron_last');
  234. $this->cronRun();
  235. $this->assertSession()->statusCodeEquals(204);
  236. $next_cron_time = \Drupal::state()->get('system.cron_last');
  237. $this->assertGreaterThan($last_cron_time, $next_cron_time);
  238. }
  239. /**
  240. * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
  241. */
  242. public function testInstall() {
  243. $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
  244. $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
  245. }
  246. }