annotate core/modules/search/tests/src/Functional/SearchTestBase.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\search\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Drupal\Component\Utility\SafeMarkup;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Defines the common search test code.
Chris@0 10 */
Chris@0 11 abstract class SearchTestBase extends BrowserTestBase {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Modules to enable.
Chris@0 15 *
Chris@0 16 * @var array
Chris@0 17 */
Chris@0 18 public static $modules = ['node', 'search', 'dblog'];
Chris@0 19
Chris@0 20 protected function setUp() {
Chris@0 21 parent::setUp();
Chris@0 22
Chris@0 23 // Create Basic page and Article node types.
Chris@0 24 if ($this->profile != 'standard') {
Chris@0 25 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@0 26 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
Chris@0 27 }
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Simulates submission of a form using GET instead of POST.
Chris@0 32 *
Chris@0 33 * Forms that use the GET method cannot be submitted with
Chris@0 34 * WebTestBase::drupalPostForm(), which explicitly uses POST to submit the
Chris@0 35 * form. So this method finds the form, verifies that it has input fields and
Chris@0 36 * a submit button matching the inputs to this method, and then calls
Chris@0 37 * WebTestBase::drupalGet() to simulate the form submission to the 'action'
Chris@0 38 * URL of the form (if set, or the current URL if not).
Chris@0 39 *
Chris@0 40 * See WebTestBase::drupalPostForm() for more detailed documentation of the
Chris@0 41 * function parameters.
Chris@0 42 *
Chris@0 43 * @param string $path
Chris@0 44 * Location of the form to be submitted: either a Drupal path, absolute
Chris@0 45 * path, or NULL to use the current page.
Chris@0 46 * @param array $edit
Chris@0 47 * Form field data to submit. Unlike drupalPostForm(), this does not support
Chris@0 48 * file uploads.
Chris@0 49 * @param string $submit
Chris@0 50 * Value of the submit button to submit clicking. Unlike drupalPostForm(),
Chris@0 51 * this does not support AJAX.
Chris@0 52 * @param string $form_html_id
Chris@0 53 * (optional) HTML ID of the form, to disambiguate.
Chris@0 54 */
Chris@0 55 protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {
Chris@0 56 if (isset($path)) {
Chris@0 57 $this->drupalGet($path);
Chris@0 58 }
Chris@0 59
Chris@0 60 if ($this->parse()) {
Chris@0 61 // Iterate over forms to find one that matches $edit and $submit.
Chris@0 62 $edit_save = $edit;
Chris@0 63 $xpath = '//form';
Chris@0 64 if (!empty($form_html_id)) {
Chris@0 65 $xpath .= "[@id='" . $form_html_id . "']";
Chris@0 66 }
Chris@0 67 $forms = $this->xpath($xpath);
Chris@0 68 foreach ($forms as $form) {
Chris@0 69 // Try to set the fields of this form as specified in $edit.
Chris@0 70 $edit = $edit_save;
Chris@0 71 $post = [];
Chris@0 72 $upload = [];
Chris@0 73 $submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form);
Chris@0 74 if (!$edit && $submit_matches) {
Chris@0 75 // Everything matched, so "submit" the form.
Chris@0 76 $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : NULL;
Chris@0 77 $this->drupalGet($action, ['query' => $post]);
Chris@0 78 return;
Chris@0 79 }
Chris@0 80 }
Chris@0 81
Chris@0 82 // We have not found a form which contained all fields of $edit and
Chris@0 83 // the submit button.
Chris@0 84 foreach ($edit as $name => $value) {
Chris@0 85 $this->fail(SafeMarkup::format('Failed to set field @name to @value', ['@name' => $name, '@value' => $value]));
Chris@0 86 }
Chris@0 87 $this->assertTrue($submit_matches, format_string('Found the @submit button', ['@submit' => $submit]));
Chris@0 88 $this->fail(format_string('Found the requested form fields at @path', ['@path' => $path]));
Chris@0 89 }
Chris@0 90 }
Chris@0 91
Chris@0 92 }