function HelpTopicsSyntaxTest::verifyTopic

Same name and namespace in other branches
  1. 9 core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php \Drupal\Tests\help_topics\Functional\HelpTopicsSyntaxTest::verifyTopic()
  2. 8.9.x core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php \Drupal\Tests\help_topics\Functional\HelpTopicsSyntaxTest::verifyTopic()
  3. 11.x core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php \Drupal\Tests\help\Functional\HelpTopicsSyntaxTest::verifyTopic()

Verifies rendering and standards compliance of one help topic.

Parameters

string $id: ID of the topic to verify.

array $definitions: Array of all topic definitions, keyed by ID.

int $response: Expected response from visiting the page for the topic.

2 calls to HelpTopicsSyntaxTest::verifyTopic()
HelpTopicsSyntaxTest::testHelpTopics in core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php
Tests that all Core help topics can be rendered and have good syntax.
HelpTopicsSyntaxTest::verifyBadTopic in core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php
Verifies that a bad topic fails in the expected way.

File

core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php, line 101

Class

HelpTopicsSyntaxTest
Verifies that all core Help topics can be rendered and comply with standards.

Namespace

Drupal\Tests\help\Functional

Code

protected function verifyTopic($id, $definitions, $response = 200) {
  $definition = $definitions[$id];
  HelpTestTwigNodeVisitor::setStateValue('manner', 0);
  // Visit the URL for the topic.
  $this->drupalGet('admin/help/topic/' . $id);
  // Verify the title and response.
  $session = $this->assertSession();
  $session->statusCodeEquals($response);
  if ($response == 200) {
    $session->titleEquals($definition['label'] . ' | Drupal');
  }
  // Verify that all the related topics exist. Also check to see if any of
  // them are top-level (we will need that in the next section).
  $has_top_level_related = FALSE;
  if (isset($definition['related'])) {
    foreach ($definition['related'] as $related_id) {
      $this->assertArrayHasKey($related_id, $definitions, 'Topic ' . $id . ' is only related to topics that exist: ' . $related_id);
      $has_top_level_related = $has_top_level_related || !empty($definitions[$related_id]['top_level']);
    }
  }
  // Verify this is either top-level or related to a top-level topic.
  $this->assertTrue(!empty($definition['top_level']) || $has_top_level_related, 'Topic ' . $id . ' is either top-level or related to at least one other top-level topic');
  // Verify that the label is not empty.
  $this->assertNotEmpty($definition['label'], 'Topic ' . $id . ' has a non-empty label');
  // Test the syntax and contents of the Twig file (without the front
  // matter, which is tested in other ways above). We need to render the
  // template several times with variations, so read it in once.
  $template = file_get_contents($definition[HelpTopicDiscovery::FILE_KEY]);
  $template_text = FrontMatter::create($template)->getContent();
  // Verify that the body is not empty and is valid HTML.
  $text = $this->renderHelpTopic($template_text, 'bare_body');
  $this->assertNotEmpty($text, 'Topic ' . $id . ' contains some text outside of front matter');
  $this->validateHtml($text, $id);
  $max_chunk_num = HelpTestTwigNodeVisitor::getState()['max_chunk'];
  $this->assertTrue($max_chunk_num >= 0, 'Topic ' . $id . ' has at least one translated chunk');
  // Verify that each chunk of the translated text is locale-safe and
  // valid HTML.
  $chunk_num = 0;
  $number_checked = 0;
  while ($chunk_num <= $max_chunk_num) {
    $chunk_str = $id . ' section ' . $chunk_num;
    // Render the topic, asking for just one chunk, and extract the chunk.
    // Note that some chunks may not actually get rendered, if they are inside
    // set statements, because we skip rendering variable output.
    HelpTestTwigNodeVisitor::setStateValue('return_chunk', $chunk_num);
    $text = $this->renderHelpTopic($template_text, 'translated_chunk');
    $matches = [];
    $matched = preg_match('|' . HelpTestTwigNodeVisitor::DELIMITER . '(.*)' . HelpTestTwigNodeVisitor::DELIMITER . '|', $text, $matches);
    if ($matched) {
      $number_checked++;
      $text = $matches[1];
      $this->assertNotEmpty($text, 'Topic ' . $chunk_str . ' contains text');
      // Verify the chunk is OK.
      $this->assertTrue(locale_string_is_safe($text), 'Topic ' . $chunk_str . ' translatable string is locale-safe');
      $this->validateHtml($text, $chunk_str);
    }
    $chunk_num++;
  }
  $this->assertTrue($number_checked > 0, 'Tested at least one translated chunk in ' . $id);
  // Validate the HTML in the body with the translated text replaced by a
  // dummy string, to verify that HTML syntax is not partly in and partly out
  // of the translated text.
  $text = $this->renderHelpTopic($template_text, 'replace_translated');
  $this->validateHtml($text, $id);
  // Verify that if we remove all the translated text, whitespace, and
  // HTML tags, there is nothing left (that is, all text is translated).
  $text = preg_replace('|\\s+|', '', $this->renderHelpTopic($template_text, 'remove_translated'));
  $this->assertEmpty($text, 'Topic ' . $id . ' Twig file has all of its text translated');
  // Verify that the Twig url() function was not used.
  $this->assertStringNotContainsString('url(', $template, 'Topic ' . $id . ' appears to use the url() function. Replace with help_topic_link() or help_topic_route(). See https://drupal.org/node/3074421');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.