getTextContent example

'administer node fields',
      'administer node display',
      'administer node form display',
      'view the administration theme',
    ]));
    $this->drupalGet('admin/structure/types/manage/article/display');
    // Verify that the order of view modes is alphabetical by visible label.     // Since the default view modes all have machine names which coincide with     // the English labels, they should appear in alphabetical order, by default     // if viewing the site in English and if no changes have been made. We will     // verify this first.     $page_text = $this->getTextContent();
    $start = strpos($page_text, 'view modes');
    $pos = $start;
    $list = ['Full content', 'RSS', 'Search index', 'Search result', 'Teaser'];
    // Verify that the order of the view modes is correct on the page.     foreach ($list as $name) {
      $new_pos = strpos($page_text$name$start);
      $this->assertGreaterThan($pos$new_pos);
      $pos = $new_pos;
    }
    // Now that we have verified the original display order, we can change the     // label for one of the view modes. If we rename "Teaser" to "Breezer", it


class TestClass extends UnitTestCase {
  use AssertContentTrait;

  public function _setRawContent($content) {
    $this->setRawContent($content);
  }

  public function _getTextContent() {
    return $this->getTextContent();
  }

}

  protected function assertPageCounts(string $path, array $counts, string $message): void {
    // Disable read more links.     \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'page', 'teaser')
      ->removeComponent('links')
      ->save();

    // Get the text of the page.     $this->drupalGet($path);
    $text = $this->getTextContent();

    // Check the counts. Note that the title and body are both shown on the     // page, and they are the same. So the title/body string should appear on     // the page twice as many times as the input count.     foreach ($counts as $langcode => $count) {
      $this->assertEquals(2 * $countsubstr_count($text$this->nodeTitles[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
    }
  }

}
$this->drupalGet('admin/people');
    $this->submitForm($edit, 'Apply to selected items');
    $this->assertSession()->pageTextContains('Are you sure you want to cancel these user accounts?');
    $this->assertSession()->pageTextContains('Cancellation method');
    $this->assertSession()->pageTextContains('Require email confirmation');
    $this->assertSession()->pageTextContains('Notify user when account is canceled');

    // Confirm deletion.     $this->submitForm([], 'Confirm');
    $status = TRUE;
    foreach ($users as $account) {
      $status = $status && (str_contains($this->getTextContent(), "Account {$account->getAccountName()} has been deleted."));
      $user_storage->resetCache([$account->id()]);
      $status = $status && !$user_storage->load($account->id());
    }
    $this->assertTrue($status, 'Users deleted and not found in the database.');

    // Ensure that admin account was not cancelled.     $this->assertSession()->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');
    $admin_user = $user_storage->load($admin_user->id());
    $this->assertTrue($admin_user->isActive(), 'Administrative user is found in the database and enabled.');

    // Verify that uid 1's account was not cancelled.


    // Ensure a module which does not provide a module overview page is handled     // correctly.     $this->clickLink(\Drupal::moduleHandler()->getName('help_test'));
    $this->assertSession()->pageTextContains('No help is available for module ' . \Drupal::moduleHandler()->getName('help_test'));

    // Verify that the order of topics is alphabetical by displayed module     // name, by checking the order of some modules, including some that would     // have a different order if it was done by machine name instead.     $this->drupalGet('admin/help');
    $page_text = $this->getTextContent();
    $start = strpos($page_text, 'Module overviews');
    $pos = $start;
    $list = ['Block', 'Block Content', 'Breakpoint', 'History', 'Text Editor'];
    foreach ($list as $name) {
      $this->assertSession()->linkExists($name);
      $new_pos = strpos($page_text$name$start);
      $this->assertGreaterThan($pos$new_pos, "Order of $name is not correct on page");
      $pos = $new_pos;
    }
  }

  

      }
    }

    // Test that the language field value is shown.     $this->assertSession()->pageTextNotContains('English');
    $this->assertSession()->pageTextContains('French');
    $this->assertSession()->pageTextContains('Spanish');

    // Test page sorting, which is by language code, ascending. So the     // Spanish nodes should appear before the French nodes.     $page = $this->getTextContent();
    $pos_es_max = 0;
    $pos_fr_min = 10000;
    foreach ($this->nodeTitles['es'] as $title) {
      $pos_es_max = max($pos_es_maxstrpos($page$title));
    }
    foreach ($this->nodeTitles['fr'] as $title) {
      $pos_fr_min = min($pos_fr_minstrpos($page$title));
    }
    $this->assertLessThan($pos_fr_min$pos_es_max, "The Spanish translation should appear before the French one on $message.");

    // Test the argument -- filter to just Spanish.
    $this->drupalLogin($this->adminUser);
    $this->verifyHelp();
    $this->verifyBreadCrumb();

    // Verify that help topics text appears on admin/help, and cache tags.     $this->drupalGet('admin/help');
    $session->responseContains('<h2>Topics</h2>');
    $session->pageTextContains('Topics can be provided by modules or themes');
    $session->responseHeaderContains('X-Drupal-Cache-Tags', 'core.extension');

    // Verify links for help topics and order.     $page_text = $this->getTextContent();
    $start = strpos($page_text, 'Topics can be provided');
    $pos = $start;
    foreach ($this->getTopicList() as $info) {
      $name = $info['name'];
      $session->linkExists($name);
      $new_pos = strpos($page_text$name$start);
      $this->assertGreaterThan($pos$new_pos, "Order of $name is not correct on page");
      $pos = $new_pos;
    }

    // Ensure the plugin manager alter hook works as expected.

      ->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");
    }
  }

}

  protected function assertTextHelper($text$message = '', $group = NULL, $not_exists = TRUE): void {
    if (!$message) {
      $message = !$not_exists ? new FormattableMarkup('"@text" found', ['@text' => $text]) : new FormattableMarkup('"@text" not found', ['@text' => $text]);
    }
    if ($not_exists) {
      $this->assertStringNotContainsString((string) $text$this->getTextContent()$message);
    }
    else {
      $this->assertStringContainsString((string) $text$this->getTextContent()$message);
    }
  }

  /** * Passes if the text is found ONLY ONCE on the text version of the page. * * The text version is the equivalent of what a user would see when viewing * through a web browser. In other words the HTML has been filtered out of * the contents. * * @param string|\Drupal\Component\Render\MarkupInterface $text * Plain text to look for. * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * * @return bool * TRUE on pass, FALSE on fail. */
'access tour',
    ]);
    $this->drupalLogin($account);
  }

  /** * Tests the order of the help page. */
  public function testHelp() {
    $pos = 0;
    $this->drupalGet('admin/help');
    $page_text = $this->getTextContent();
    foreach ($this->stringOrder as $item) {
      $new_pos = strpos($page_text$item$pos);
      $this->assertGreaterThan($pos$new_pos, "Order of $item is not correct on help page");
      $pos = $new_pos;
    }
  }

}

  protected function assertPageCounts(string $path, array $counts, string $message): void {
    // Get the text of the page.     $this->drupalGet($path);
    $text = $this->getTextContent();

    // Check the counts. Note that the title and body are both shown on the     // page, and they are the same. So the title/body string should appear on     // the page twice as many times as the input count.     foreach ($counts as $langcode => $count) {
      $this->assertEquals(2 * $countsubstr_count($text$this->commentTitles[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
    }
  }

}

  protected function assertPageCounts(string $path, array $counts, string $message): void {
    // Get the text of the page.     $this->drupalGet($path);
    $text = $this->getTextContent();

    // Check the counts. Note that the title and body are both shown on the     // page, and they are the same. So the title/body string should appear on     // the page twice as many times as the input count.     foreach ($counts as $langcode => $count) {
      $this->assertEquals(2 * $countsubstr_count($text$this->termNames[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
    }
  }

  /** * Creates a taxonomy term with specified name and other properties. * * @param array $properties * Array of properties and field values to set. * * @return \Drupal\taxonomy\TermInterface * The created taxonomy term. */
$account = $this->drupalCreateUser();
    $this->drupalLogin($account);

    // Visit a Drupal page that requires login.     $this->drupalGet('test-page');
    $this->assertSession()->statusCodeEquals(200);

    // Test page contains some text.     $this->assertSession()->pageTextContains('Test page text.');

    // Check that returned plain text is correct.     $text = $this->getTextContent();
    $this->assertStringContainsString('Test page text.', $text);
    $this->assertStringNotContainsString('</html>', $text);
    // Ensure Drupal Javascript settings are not part of the page text.     $this->assertArrayHasKey('currentPathIsAdmin', $this->getDrupalSettings()['path']);
    $this->assertStringNotContainsString('currentPathIsAdmin', $text);

    // Response includes cache tags that we can assert.     $this->assertSession()->responseHeaderExists('X-Drupal-Cache-Tags');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');

    // Test that we can read the JS settings.

  protected function assertPageCounts(string $path, array $counts, string $message): void {
    // Get the text of the page.     $this->drupalGet($path);
    $text = $this->getTextContent();

    foreach ($counts as $langcode => $count) {
      $this->assertEquals($countsubstr_count($text$this->blockContentInfos[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.