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