class AddFeedTest

Same name in this branch
  1. 9 core/modules/aggregator/tests/src/Functional/AddFeedTest.php \Drupal\Tests\aggregator\Functional\AddFeedTest
Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Kernel/Common/AddFeedTest.php \Drupal\Tests\system\Kernel\Common\AddFeedTest
  2. 10 core/modules/system/tests/src/Kernel/Common/AddFeedTest.php \Drupal\Tests\system\Kernel\Common\AddFeedTest
  3. 8.9.x core/modules/system/tests/src/Kernel/Common/AddFeedTest.php \Drupal\Tests\system\Kernel\Common\AddFeedTest
  4. 8.9.x core/modules/aggregator/tests/src/Functional/AddFeedTest.php \Drupal\Tests\aggregator\Functional\AddFeedTest

Make sure that attaching feeds works correctly with various constructs.

@group Common

Hierarchy

Expanded class hierarchy of AddFeedTest

File

core/modules/system/tests/src/Kernel/Common/AddFeedTest.php, line 13

Namespace

Drupal\Tests\system\Kernel\Common
View source
class AddFeedTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
  ];
  
  /**
   * Tests attaching feeds with paths, URLs, and titles.
   */
  public function testBasicFeedAddNoTitle() {
    $path = $this->randomMachineName(12);
    $external_url = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
    $fully_qualified_local_url = Url::fromUri('base:' . $this->randomMachineName(12), [
      'absolute' => TRUE,
    ])
      ->toString();
    $path_for_title = $this->randomMachineName(12);
    $external_for_title = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12);
    $fully_qualified_for_title = Url::fromUri('base:' . $this->randomMachineName(12), [
      'absolute' => TRUE,
    ])
      ->toString();
    $urls = [
      'path without title' => [
        'url' => Url::fromUri('base:' . $path, [
          'absolute' => TRUE,
        ])->toString(),
        'title' => '',
      ],
      'external URL without title' => [
        'url' => $external_url,
        'title' => '',
      ],
      'local URL without title' => [
        'url' => $fully_qualified_local_url,
        'title' => '',
      ],
      'path with title' => [
        'url' => Url::fromUri('base:' . $path_for_title, [
          'absolute' => TRUE,
        ])->toString(),
        'title' => $this->randomMachineName(12),
      ],
      'external URL with title' => [
        'url' => $external_for_title,
        'title' => $this->randomMachineName(12),
      ],
      'local URL with title' => [
        'url' => $fully_qualified_for_title,
        'title' => $this->randomMachineName(12),
      ],
    ];
    $build = [];
    foreach ($urls as $feed_info) {
      $build['#attached']['feed'][] = [
        $feed_info['url'],
        $feed_info['title'],
      ];
    }
    // Use the bare HTML page renderer to render our links.
    $renderer = $this->container
      ->get('bare_html_page_renderer');
    $response = $renderer->renderBarePage($build, '', 'maintenance_page');
    // Glean the content from the response object.
    $this->setRawContent($response->getContent());
    // Assert that the content contains the RSS links we specified.
    foreach ($urls as $description => $feed_info) {
      $this->assertPattern($this->urlToRSSLinkPattern($feed_info['url'], $feed_info['title']));
    }
  }
  
  /**
   * Creates a pattern representing the RSS feed in the page.
   */
  public function urlToRSSLinkPattern($url, $title = '') {
    // Escape any regular expression characters in the URL ('?' is the worst).
    $url = preg_replace('/([+?.*])/', '[$0]', $url);
    $generated_pattern = '%<link +href="' . $url . '" +rel="alternate" +title="' . $title . '" +type="application/rss.xml" */>%';
    return $generated_pattern;
  }
  
  /**
   * Checks that special characters are correctly escaped.
   *
   * @see https://www.drupal.org/node/1211668
   */
  public function testFeedIconEscaping() {
    $variables = [
      '#theme' => 'feed_icon',
      '#url' => 'node',
      '#title' => '<>&"\'',
    ];
    $text = \Drupal::service('renderer')->renderRoot($variables);
    $this->assertEquals('Subscribe to &lt;&gt;&amp;&quot;&#039;', trim(strip_tags($text)), 'feed_icon template escapes reserved HTML characters.');
  }

}

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