AddFeedTest.php 3.75 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\Tests\AddFeedTest.
6
7
8
9
10
 */

namespace Drupal\aggregator\Tests;

/**
11
12
13
 * Add feed test.
 *
 * @group aggregator
14
15
 */
class AddFeedTest extends AggregatorTestBase {
16
17
18
19
20
21
22

  protected function setUp() {
    parent::setUp();

    $this->drupalPlaceBlock('page_title_block');
  }

23
  /**
24
   * Creates and ensures that a feed is unique, checks source, and deletes feed.
25
   */
26
  public function testAddFeed() {
27
    $feed = $this->createFeed();
28
    $feed->refreshItems();
29
30

    // Check feed data.
31
    $this->assertUrl(\Drupal::url('aggregator.feed_add', [], ['absolute' => TRUE]), [], 'Directed to correct url.');
32
    $this->assertTrue($this->uniqueFeed($feed->label(), $feed->getUrl()), 'The feed is unique.');
33
34

    // Check feed source.
35
    $this->drupalGet('aggregator/sources/' . $feed->id());
36
    $this->assertResponse(200, 'Feed source exists.');
37
    $this->assertText($feed->label(), 'Page title');
38
    $this->assertRaw($feed->getWebsiteUrl());
39

40
41
42
43
44
45
46
47
48
49
    // Try to add a duplicate.
    $edit = [
      'title[0][value]' => $feed->label(),
      'url[0][value]' => $feed->getUrl(),
      'refresh' => '900',
    ];
    $this->drupalPostForm('aggregator/sources/add', $edit, t('Save'));
    $this->assertRaw(t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
    $this->assertRaw(t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->getUrl())));

50
51
52
53
    // Delete feed.
    $this->deleteFeed($feed);
  }

54
55
56
57
58
59
60
61
62
63
  /**
   * Ensures that the feed label is escaping when rendering the feed icon.
   */
  public function testFeedLabelEscaping() {
    $feed = $this->createFeed(NULL, ['title[0][value]' => 'Test feed title <script>alert(123);</script>']);
    $this->checkForMetaRefresh();

    $this->drupalGet('aggregator/sources/' . $feed->id());
    $this->assertResponse(200);

64
65
    $this->assertEscaped('Test feed title <script>alert(123);</script>');
    $this->assertNoRaw('Test feed title <script>alert(123);</script>');
66
67
68
69
70

    // Ensure the feed icon title is escaped.
    $this->assertTrue(strpos(str_replace(["\n", "\r"], '', $this->getRawContent()), 'class="feed-icon">  Subscribe to Test feed title &lt;script&gt;alert(123);&lt;/script&gt; feed</a>') !== FALSE);
  }

71
72
73
  /**
   * Tests feeds with very long URLs.
   */
74
  public function testAddLongFeed() {
75
76
77
    // Create a feed with a URL of > 255 characters.
    $long_url = "https://www.google.com/search?ix=heb&sourceid=chrome&ie=UTF-8&q=angie+byron#sclient=psy-ab&hl=en&safe=off&source=hp&q=angie+byron&pbx=1&oq=angie+byron&aq=f&aqi=&aql=&gs_sm=3&gs_upl=0l0l0l10534l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=a70b6b1f0abe28d8&biw=1629&bih=889&ix=heb";
    $feed = $this->createFeed($long_url);
78
    $feed->refreshItems();
79
80
81
82
83

    // Create a second feed of > 255 characters, where the only difference is
    // after the 255th character.
    $long_url_2 = "https://www.google.com/search?ix=heb&sourceid=chrome&ie=UTF-8&q=angie+byron#sclient=psy-ab&hl=en&safe=off&source=hp&q=angie+byron&pbx=1&oq=angie+byron&aq=f&aqi=&aql=&gs_sm=3&gs_upl=0l0l0l10534l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=a70b6b1f0abe28d8&biw=1629&bih=889";
    $feed_2 = $this->createFeed($long_url_2);
84
    $feed->refreshItems();
85
86

    // Check feed data.
87
88
    $this->assertTrue($this->uniqueFeed($feed->label(), $feed->getUrl()), 'The first long URL feed is unique.');
    $this->assertTrue($this->uniqueFeed($feed_2->label(), $feed_2->getUrl()), 'The second long URL feed is unique.');
89
90

    // Check feed source.
91
    $this->drupalGet('aggregator/sources/' . $feed->id());
92
    $this->assertResponse(200, 'Long URL feed source exists.');
93
    $this->assertText($feed->label(), 'Page title');
94
95
96
97
98
99

    // Delete feeds.
    $this->deleteFeed($feed);
    $this->deleteFeed($feed_2);
  }
}