1. 8.2.x core/modules/aggregator/src/Tests/AggregatorTestBase.php
  2. 8.0.x core/modules/aggregator/src/Tests/AggregatorTestBase.php
  3. 8.1.x core/modules/aggregator/src/Tests/AggregatorTestBase.php
  4. 8.3.x core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php
  5. 8.3.x core/modules/aggregator/src/Tests/AggregatorTestBase.php

Namespace

Drupal\aggregator\Tests

File

core/modules/aggregator/src/Tests/AggregatorTestBase.php
View source
  1. <?php
  2. namespace Drupal\aggregator\Tests;
  3. use Drupal\aggregator\Entity\Feed;
  4. use Drupal\Component\Utility\Html;
  5. use Drupal\simpletest\WebTestBase;
  6. use Drupal\aggregator\FeedInterface;
  7. /**
  8. * Defines a base class for testing the Aggregator module.
  9. */
  10. abstract class AggregatorTestBase extends WebTestBase {
  11. /**
  12. * A user with permission to administer feeds and create content.
  13. *
  14. * @var \Drupal\user\Entity\User
  15. */
  16. protected $adminUser;
  17. /**
  18. * Modules to install.
  19. *
  20. * @var array
  21. */
  22. public static $modules = ['block', 'node', 'aggregator', 'aggregator_test', 'views'];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. // Create an Article node type.
  29. if ($this->profile != 'standard') {
  30. $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
  31. }
  32. $this->adminUser = $this->drupalCreateUser(array('access administration pages', 'administer news feeds', 'access news feeds', 'create article content'));
  33. $this->drupalLogin($this->adminUser);
  34. $this->drupalPlaceBlock('local_tasks_block');
  35. }
  36. /**
  37. * Creates an aggregator feed.
  38. *
  39. * This method simulates the form submission on path aggregator/sources/add.
  40. *
  41. * @param string $feed_url
  42. * (optional) If given, feed will be created with this URL, otherwise
  43. * /rss.xml will be used. Defaults to NULL.
  44. * @param array $edit
  45. * Array with additional form fields.
  46. *
  47. * @return \Drupal\aggregator\FeedInterface
  48. * Full feed object if possible.
  49. *
  50. * @see getFeedEditArray()
  51. */
  52. public function createFeed($feed_url = NULL, array $edit = array()) {
  53. $edit = $this->getFeedEditArray($feed_url, $edit);
  54. $this->drupalPostForm('aggregator/sources/add', $edit, t('Save'));
  55. $this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title[0][value]'])), format_string('The feed @name has been added.', array('@name' => $edit['title[0][value]'])));
  56. $fid = db_query("SELECT fid FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $edit['title[0][value]'], ':url' => $edit['url[0][value]']))->fetchField();
  57. $this->assertTrue(!empty($fid), 'The feed found in database.');
  58. return Feed::load($fid);
  59. }
  60. /**
  61. * Deletes an aggregator feed.
  62. *
  63. * @param \Drupal\aggregator\FeedInterface $feed
  64. * Feed object representing the feed.
  65. */
  66. public function deleteFeed(FeedInterface $feed) {
  67. $this->drupalPostForm('aggregator/sources/' . $feed->id() . '/delete', array(), t('Delete'));
  68. $this->assertRaw(t('The feed %title has been deleted.', array('%title' => $feed->label())), 'Feed deleted successfully.');
  69. }
  70. /**
  71. * Returns a randomly generated feed edit array.
  72. *
  73. * @param string $feed_url
  74. * (optional) If given, feed will be created with this URL, otherwise
  75. * /rss.xml will be used. Defaults to NULL.
  76. * @param array $edit
  77. * Array with additional form fields.
  78. *
  79. * @return array
  80. * A feed array.
  81. */
  82. public function getFeedEditArray($feed_url = NULL, array $edit = array()) {
  83. $feed_name = $this->randomMachineName(10);
  84. if (!$feed_url) {
  85. $feed_url = \Drupal::url('view.frontpage.feed_1', array(), array(
  86. 'query' => array('feed' => $feed_name),
  87. 'absolute' => TRUE,
  88. ));
  89. }
  90. $edit += array(
  91. 'title[0][value]' => $feed_name,
  92. 'url[0][value]' => $feed_url,
  93. 'refresh' => '900',
  94. );
  95. return $edit;
  96. }
  97. /**
  98. * Returns a randomly generated feed edit object.
  99. *
  100. * @param string $feed_url
  101. * (optional) If given, feed will be created with this URL, otherwise
  102. * /rss.xml will be used. Defaults to NULL.
  103. * @param array $values
  104. * (optional) Default values to initialize object properties with.
  105. *
  106. * @return \Drupal\aggregator\FeedInterface
  107. * A feed object.
  108. */
  109. public function getFeedEditObject($feed_url = NULL, array $values = array()) {
  110. $feed_name = $this->randomMachineName(10);
  111. if (!$feed_url) {
  112. $feed_url = \Drupal::url('view.frontpage.feed_1', array(
  113. 'query' => array('feed' => $feed_name),
  114. 'absolute' => TRUE,
  115. ));
  116. }
  117. $values += array(
  118. 'title' => $feed_name,
  119. 'url' => $feed_url,
  120. 'refresh' => '900',
  121. );
  122. return Feed::create($values);
  123. }
  124. /**
  125. * Returns the count of the randomly created feed array.
  126. *
  127. * @return int
  128. * Number of feed items on default feed created by createFeed().
  129. */
  130. public function getDefaultFeedItemCount() {
  131. // Our tests are based off of rss.xml, so let's find out how many elements should be related.
  132. $feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
  133. return $feed_count > 10 ? 10 : $feed_count;
  134. }
  135. /**
  136. * Updates the feed items.
  137. *
  138. * This method simulates a click to
  139. * admin/config/services/aggregator/update/$fid.
  140. *
  141. * @param \Drupal\aggregator\FeedInterface $feed
  142. * Feed object representing the feed.
  143. * @param int|null $expected_count
  144. * Expected number of feed items. If omitted no check will happen.
  145. */
  146. public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
  147. // First, let's ensure we can get to the rss xml.
  148. $this->drupalGet($feed->getUrl());
  149. $this->assertResponse(200, format_string(':url is reachable.', array(':url' => $feed->getUrl())));
  150. // Attempt to access the update link directly without an access token.
  151. $this->drupalGet('admin/config/services/aggregator/update/' . $feed->id());
  152. $this->assertResponse(403);
  153. // Refresh the feed (simulated link click).
  154. $this->drupalGet('admin/config/services/aggregator');
  155. $this->clickLink('Update items');
  156. // Ensure we have the right number of items.
  157. $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()));
  158. $feed->items = array();
  159. foreach ($result as $item) {
  160. $feed->items[] = $item->iid;
  161. }
  162. if ($expected_count !== NULL) {
  163. $feed->item_count = count($feed->items);
  164. $this->assertEqual($expected_count, $feed->item_count, format_string('Total items in feed equal to the total items in database (@val1 != @val2)', array('@val1' => $expected_count, '@val2' => $feed->item_count)));
  165. }
  166. }
  167. /**
  168. * Confirms an item removal from a feed.
  169. *
  170. * @param \Drupal\aggregator\FeedInterface $feed
  171. * Feed object representing the feed.
  172. */
  173. public function deleteFeedItems(FeedInterface $feed) {
  174. $this->drupalPostForm('admin/config/services/aggregator/delete/' . $feed->id(), array(), t('Delete items'));
  175. $this->assertRaw(t('The news items from %title have been deleted.', array('%title' => $feed->label())), 'Feed items deleted.');
  176. }
  177. /**
  178. * Adds and deletes feed items and ensure that the count is zero.
  179. *
  180. * @param \Drupal\aggregator\FeedInterface $feed
  181. * Feed object representing the feed.
  182. * @param int $expected_count
  183. * Expected number of feed items.
  184. */
  185. public function updateAndDelete(FeedInterface $feed, $expected_count) {
  186. $this->updateFeedItems($feed, $expected_count);
  187. $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField();
  188. $this->assertTrue($count);
  189. $this->deleteFeedItems($feed);
  190. $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField();
  191. $this->assertTrue($count == 0);
  192. }
  193. /**
  194. * Checks whether the feed name and URL are unique.
  195. *
  196. * @param string $feed_name
  197. * String containing the feed name to check.
  198. * @param string $feed_url
  199. * String containing the feed url to check.
  200. *
  201. * @return bool
  202. * TRUE if feed is unique.
  203. */
  204. public function uniqueFeed($feed_name, $feed_url) {
  205. $result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed_name, ':url' => $feed_url))->fetchField();
  206. return (1 == $result);
  207. }
  208. /**
  209. * Creates a valid OPML file from an array of feeds.
  210. *
  211. * @param array $feeds
  212. * An array of feeds.
  213. *
  214. * @return string
  215. * Path to valid OPML file.
  216. */
  217. public function getValidOpml(array $feeds) {
  218. // Properly escape URLs so that XML parsers don't choke on them.
  219. foreach ($feeds as &$feed) {
  220. $feed['url[0][value]'] = Html::escape($feed['url[0][value]']);
  221. }
  222. /**
  223. * Does not have an XML declaration, must pass the parser.
  224. */
  225. $opml = <<<EOF
  226. <opml version="1.0">
  227. <head></head>
  228. <body>
  229. <!-- First feed to be imported. -->
  230. <outline text="{$feeds[0]['title[0][value]']}" xmlurl="{$feeds[0]['url[0][value]']}" />
  231. <!-- Second feed. Test string delimitation and attribute order. -->
  232. <outline xmlurl='{$feeds[1]['url[0][value]']}' text='{$feeds[1]['title[0][value]']}'/>
  233. <!-- Test for duplicate URL and title. -->
  234. <outline xmlurl="{$feeds[0]['url[0][value]']}" text="Duplicate URL"/>
  235. <outline xmlurl="http://duplicate.title" text="{$feeds[1]['title[0][value]']}"/>
  236. <!-- Test that feeds are only added with required attributes. -->
  237. <outline text="{$feeds[2]['title[0][value]']}" />
  238. <outline xmlurl="{$feeds[2]['url[0][value]']}" />
  239. </body>
  240. </opml>
  241. EOF;
  242. $path = 'public://valid-opml.xml';
  243. return file_unmanaged_save_data($opml, $path);
  244. }
  245. /**
  246. * Creates an invalid OPML file.
  247. *
  248. * @return string
  249. * Path to invalid OPML file.
  250. */
  251. public function getInvalidOpml() {
  252. $opml = <<<EOF
  253. <opml>
  254. <invalid>
  255. </opml>
  256. EOF;
  257. $path = 'public://invalid-opml.xml';
  258. return file_unmanaged_save_data($opml, $path);
  259. }
  260. /**
  261. * Creates a valid but empty OPML file.
  262. *
  263. * @return string
  264. * Path to empty OPML file.
  265. */
  266. public function getEmptyOpml() {
  267. $opml = <<<EOF
  268. <?xml version="1.0" encoding="utf-8"?>
  269. <opml version="1.0">
  270. <head></head>
  271. <body>
  272. <outline text="Sample text" />
  273. <outline text="Sample text" url="Sample URL" />
  274. </body>
  275. </opml>
  276. EOF;
  277. $path = 'public://empty-opml.xml';
  278. return file_unmanaged_save_data($opml, $path);
  279. }
  280. /**
  281. * Returns a example RSS091 feed.
  282. *
  283. * @return string
  284. * Path to the feed.
  285. */
  286. public function getRSS091Sample() {
  287. return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_rss091.xml';
  288. }
  289. /**
  290. * Returns a example Atom feed.
  291. *
  292. * @return string
  293. * Path to the feed.
  294. */
  295. public function getAtomSample() {
  296. // The content of this sample ATOM feed is based directly off of the
  297. // example provided in RFC 4287.
  298. return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_atom.xml';
  299. }
  300. /**
  301. * Returns a example feed.
  302. *
  303. * @return string
  304. * Path to the feed.
  305. */
  306. public function getHtmlEntitiesSample() {
  307. return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_title_entities.xml';
  308. }
  309. /**
  310. * Creates sample article nodes.
  311. *
  312. * @param int $count
  313. * (optional) The number of nodes to generate. Defaults to five.
  314. */
  315. public function createSampleNodes($count = 5) {
  316. // Post $count article nodes.
  317. for ($i = 0; $i < $count; $i++) {
  318. $edit = array();
  319. $edit['title[0][value]'] = $this->randomMachineName();
  320. $edit['body[0][value]'] = $this->randomMachineName();
  321. $this->drupalPostForm('node/add/article', $edit, t('Save'));
  322. }
  323. }
  324. /**
  325. * Enable the plugins coming with aggregator_test module.
  326. */
  327. public function enableTestPlugins() {
  328. $this->config('aggregator.settings')
  329. ->set('fetcher', 'aggregator_test_fetcher')
  330. ->set('parser', 'aggregator_test_parser')
  331. ->set('processors', array(
  332. 'aggregator_test_processor' => 'aggregator_test_processor',
  333. 'aggregator' => 'aggregator',
  334. ))
  335. ->save();
  336. }
  337. }

Classes

Namesort descending Description
AggregatorTestBase Defines a base class for testing the Aggregator module.