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