pageTextMatchesCount example

$this->assertSession()->pageTextContains('Contact form ' . $label . ' has been added.');

    // Verify that the creation message contains a link to a contact form.     $this->assertSession()->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "contact/")]');

    // Check that the form was created in site default language.     $langcode = $this->config('contact.form.' . $id)->get('langcode');
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
    $this->assertEquals($default_langcode$langcode);

    // Make sure the newly created form is included in the list of forms.     $this->assertSession()->pageTextMatchesCount(2, '/' . $label . '/');

    // Ensure that the recipient email is escaped on the listing.     $this->drupalGet('admin/structure/contact');
    $this->assertSession()->assertEscaped($recipients[0]);

    // Test update contact form.     $this->updateContactForm($id$label = $this->randomMachineName(16)implode(',', [$recipients[0]$recipients[1]])$reply = $this->randomMachineName(30), FALSE, 'Your message has been sent.', '/user');
    $config = $this->config('contact.form.' . $id)->get();
    $this->assertEquals($label$config['label']);
    $this->assertEquals([$recipients[0]$recipients[1]]$config['recipients']);
    $this->assertEquals($reply$config['reply']);
    

  protected $defaultTheme = 'stark';

  /** * Tests adding a display. */
  public function testAddDisplay() {
    $view = $this->randomView();
    $this->assertSession()->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-1"]');
    $this->assertSession()->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-2"]');
    $this->assertSession()->pageTextMatchesCount(0, '/Block name:/');

    $this->submitForm([], 'Add Block');
    $this->assertSession()->elementTextContains('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-1"]', 'Block*');
    $this->assertSession()->elementNotExists('xpath', '//li[@data-drupal-selector="edit-displays-top-tabs-block-2"]');
    $this->assertSession()->pageTextMatchesCount(1, '/Block name:/');
  }

  /** * Tests reordering of displays. */
  public function testReorderDisplay() {
    
$block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-' . $display);

    // Set label to display on the exposed filter form block.     $block->getPlugin()->setConfigurationValue('label_display', TRUE);
    $block->save();

    // Assert that the only two occurrences of `$view->getTitle()` are the title     // and h2 tags.     $this->drupalGet('test_exposed_block');
    $this->assertSession()->elementContains('css', 'title', $view->getTitle());
    $this->assertSession()->elementExists('xpath', '//h2[text()="' . $view->getTitle() . '"]');
    $this->assertSession()->pageTextMatchesCount(2, '/' . $view->getTitle() . '/');

    // Set a custom label on the exposed filter form block.     $block->getPlugin()->setConfigurationValue('views_label', '<strong>Custom</strong> title<script>alert("hacked!");</script>');
    $block->save();

    // Test that the content block label is found.     $this->drupalGet('test_exposed_block');
    $this->assertSession()->responseContains('<strong>Custom</strong> titlealert("hacked!");');

    // Set label to hidden on the exposed filter form block.     $block->getPlugin()->setConfigurationValue('label_display', FALSE);
    
/** * Checks that current page contains text only once. * * @param string $text * The string to look for. * * @see \Behat\Mink\WebAssert::pageTextContains() */
  public function pageTextContainsOnce($text) {
    $regex = '/' . preg_quote($text, '/') . '/ui';
    try {
      $this->pageTextMatchesCount(1, $regex);
    }
    catch (AssertionFailedError $e) {
      throw new ResponseTextException($e->getMessage()$this->session->getDriver());
    }
  }

  /** * Asserts that each HTML ID is used for just a single element on the page. * * @throws \Behat\Mink\Exception\ExpectationException */
  
/** * Tests Messenger service. */
  public function testDrupalMessengerService() {
    // The page at system_test.messenger_service route sets two messages and     // then removes the first before it is displayed.     $this->drupalGet(Url::fromRoute('system_test.messenger_service'));
    $this->assertSession()->pageTextNotContains('First message (removed).');
    $this->assertSession()->responseContains('Second message with <em>markup!</em> (not removed).');

    // Ensure duplicate messages are handled as expected.     $this->assertSession()->pageTextMatchesCount(1, '/Non Duplicated message/');
    $this->assertSession()->pageTextMatchesCount(3, '/Duplicated message/');

    // Ensure Markup objects are rendered as expected.     $this->assertSession()->responseContains('Markup with <em>markup!</em>');
    $this->assertSession()->pageTextMatchesCount(1, '/Markup with markup!/');
    $this->assertSession()->responseContains('Markup2 with <em>markup!</em>');

    // Ensure when the same message is of different types it is not duplicated.     $this->assertSession()->pageTextMatchesCount(1, '$Non duplicate Markup / string.$');
    $this->assertSession()->pageTextMatchesCount(2, '$Duplicate Markup / string.$');

    
$xml_mapping = [
      'drupal' => '0.0',
      'aaa_update_test' => '1_0',
      'bbb_update_test' => 'does-not-exist',
      'ccc_update_test' => '1_0',
    ];
    $this->refreshUpdateStatus($xml_mapping);

    $this->assertSession()->pageTextContains('Up to date');
    // We're expecting the report to say most projects are up to date, so we     // hope that 'Up to date' is not unique.     $this->assertSession()->pageTextMatchesCount(3, '/Up to date/');
    // It should say we failed to get data, not that we're missing an update.     $this->assertSession()->pageTextNotContains('Update available');

    // We need to check that this string is found as part of a project row, not     // just in the "Failed to get available update data" message at the top of     // the page.     $this->assertSession()->responseContains('<div class="project-update__status">Failed to get available update data');

    // We should see the output messages from fetching manually.     $this->assertSession()->pageTextContainsOnce('Checked available update data for 3 projects.');
    $this->assertSession()->pageTextContainsOnce('Failed to get available update data for one project.');

    
$this->assertSession()->responseHeaderDoesNotExist('Null-Header');
  }

  /** * @covers ::pageTextMatchesCount */
  public function testPageTextMatchesCount() {
    $this->drupalLogin($this->drupalCreateUser());

    // Visit a Drupal page that requires login.     $this->drupalGet('test-page');
    $this->assertSession()->pageTextMatchesCount(1, '/Test page text\./');

    $this->expectException(AssertionFailedError::class);
    $this->expectExceptionMessage("Failed asserting that the page matches the pattern '/does-not-exist/' 1 time(s), 0 found.");
    $this->assertSession()->pageTextMatchesCount(1, '/does-not-exist/');
  }

  /** * @covers ::pageTextContainsOnce */
  public function testPageTextContainsOnce() {
    $this->drupalLogin($this->drupalCreateUser());

    
\Drupal::service('entity_display.repository')->getFormDisplay($this->field['entity_type']$this->field['bundle'], 'default')
      ->setComponent($field_name[
        'type' => $widget,
      ])
      ->save();

    // We need to rebuild hook information after setting the component through     // the API.     $this->rebuildAll();

    $this->drupalGet('entity_test/add');
    $this->assertSession()->pageTextMatchesCount(1, '/From ' . $hook . '.* prefix on ' . $field_name . ' parent element\./');
    if ($widget === 'test_field_widget_multiple_single_value') {
      $suffix_text = "From $hook(): suffix on $field_name child element.";
      $this->assertEquals($field_storage['cardinality']substr_count($this->getTextContent()$suffix_text), "'$suffix_text' was found {$field_storage['cardinality']} times using widget $widget");
    }
  }

}
$display['display_options']['filters']['tid_2']['id'] = 'tid_2';
    $display['display_options']['filters']['tid_2']['value'][0] = $this->terms[2][0]->id();
    $display['display_options']['filters']['tid_2']['operator'] = 'or';
    $display['display_options']['filters']['tid_2']['group'] = 2;
    $view->save();

    $this->drupalGet('test-filter-taxonomy-index-tid');
    // We expect all the tagged nodes but not the untagged node.     $this->assertSession()->pageTextContainsOnce($node_with_term_1_0->label());
    // The view does not have DISTINCT query enabled, the node tagged with     // both 1.0 and 1.1 will appear twice.     $this->assertSession()->pageTextMatchesCount(2, "/{$node_with_terms_1_0_and_1_1->label()}/");
    $this->assertSession()->pageTextContainsOnce($node_with_term_2_0->label());
    $this->assertSession()->pageTextNotContains($node_no_term->label());

    // Case 3:     // - filter "tid" with a single term as "is none of"     // - filter "tid_2" with a single term as "is one of"     $view = View::load('test_filter_taxonomy_index_tid');
    $display =& $view->getDisplay('default');
    $display['display_options']['filters']['tid']['value'] = [];
    $display['display_options']['filters']['tid']['value'][0] = $this->terms[1][0]->id();
    $display['display_options']['filters']['tid']['operator'] = 'not';
    
Home | Imprint | This part of the site doesn't use cookies.