Unverified Commit 1da1c2bf authored by alexpott's avatar alexpott
Browse files

Issue #3012599 by mondrake, andypost: Replace all db calls to aggregator_feed...

Issue #3012599 by mondrake, andypost: Replace all db calls to aggregator_feed and aggregator_item tables with Entity APIs
parent 5123f756
......@@ -75,9 +75,9 @@ public function createFeed($feed_url = NULL, array $edit = []) {
$view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'aggregator/sources/']);
$this->assert(isset($view_link), 'The message area contains a link to a feed');
$fid = db_query("SELECT fid FROM {aggregator_feed} WHERE title = :title AND url = :url", [':title' => $edit['title[0][value]'], ':url' => $edit['url[0][value]']])->fetchField();
$this->assertTrue(!empty($fid), 'The feed found in database.');
return Feed::load($fid);
$fids = \Drupal::entityQuery('aggregator_feed')->condition('title', $edit['title[0][value]'])->condition('url', $edit['url[0][value]'])->execute();
$this->assertNotEmpty($fids, 'The feed found in database.');
return Feed::load(array_values($fids)[0]);
}
/**
......@@ -184,10 +184,10 @@ public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
$this->clickLink('Update items');
// Ensure we have the right number of items.
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()]);
$iids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
$feed->items = [];
foreach ($result as $item) {
$feed->items[] = $item->iid;
foreach ($iids as $iid) {
$feed->items[] = $iid;
}
if ($expected_count !== NULL) {
......@@ -216,11 +216,12 @@ public function deleteFeedItems(FeedInterface $feed) {
* Expected number of feed items.
*/
public function updateAndDelete(FeedInterface $feed, $expected_count) {
$count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
$this->updateFeedItems($feed, $expected_count);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$count = $count_query->execute();
$this->assertTrue($count);
$this->deleteFeedItems($feed);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$count = $count_query->execute();
$this->assertTrue($count == 0);
}
......@@ -236,7 +237,7 @@ public function updateAndDelete(FeedInterface $feed, $expected_count) {
* TRUE if feed is unique.
*/
public function uniqueFeed($feed_name, $feed_url) {
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", [':title' => $feed_name, ':url' => $feed_url])->fetchField();
$result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed_name)->condition('url', $feed_url)->count()->execute();
return (1 == $result);
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\Core\Database\Database;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
......@@ -21,32 +20,23 @@ public function testCron() {
// Create feed and test basic updating on cron.
$this->createSampleNodes();
$feed = $this->createFeed();
$count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField());
$this->assertEqual(5, $count_query->execute());
$this->deleteFeedItems($feed);
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField());
$this->assertEqual(0, $count_query->execute());
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField());
$this->assertEqual(5, $count_query->execute());
// Test feed locking when queued for update.
$this->deleteFeedItems($feed);
$connection = Database::getConnection();
$connection->update('aggregator_feed')
->condition('fid', $feed->id())
->fields([
'queued' => REQUEST_TIME,
])
->execute();
$feed->setQueuedTime(REQUEST_TIME)->save();
$this->cronRun();
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField());
$connection->update('aggregator_feed')
->condition('fid', $feed->id())
->fields([
'queued' => 0,
])
->execute();
$this->assertEqual(0, $count_query->execute());
$feed->setQueuedTime(0)->save();
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField());
$this->assertEqual(5, $count_query->execute());
}
}
......@@ -68,9 +68,9 @@ public function createFeed($feed_url = NULL, array $edit = []) {
$view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'aggregator/sources/']);
$this->assert(isset($view_link), 'The message area contains a link to a feed');
$fid = db_query("SELECT fid FROM {aggregator_feed} WHERE title = :title AND url = :url", [':title' => $edit['title[0][value]'], ':url' => $edit['url[0][value]']])->fetchField();
$this->assertTrue(!empty($fid), 'The feed found in database.');
return Feed::load($fid);
$fids = \Drupal::entityQuery('aggregator_feed')->condition('title', $edit['title[0][value]'])->condition('url', $edit['url[0][value]'])->execute();
$this->assertNotEmpty($fids, 'The feed found in database.');
return Feed::load(array_values($fids)[0]);
}
/**
......@@ -177,10 +177,10 @@ public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
$this->clickLink('Update items');
// Ensure we have the right number of items.
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()]);
$iids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
$feed->items = [];
foreach ($result as $item) {
$feed->items[] = $item->iid;
foreach ($iids as $iid) {
$feed->items[] = $iid;
}
if ($expected_count !== NULL) {
......@@ -209,11 +209,12 @@ public function deleteFeedItems(FeedInterface $feed) {
* Expected number of feed items.
*/
public function updateAndDelete(FeedInterface $feed, $expected_count) {
$count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
$this->updateFeedItems($feed, $expected_count);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$count = $count_query->execute();
$this->assertTrue($count);
$this->deleteFeedItems($feed);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$count = $count_query->execute();
$this->assertTrue($count == 0);
}
......@@ -229,7 +230,7 @@ public function updateAndDelete(FeedInterface $feed, $expected_count) {
* TRUE if feed is unique.
*/
public function uniqueFeed($feed_name, $feed_url) {
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", [':title' => $feed_name, ':url' => $feed_url])->fetchField();
$result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed_name)->condition('url', $feed_url)->count()->execute();
return (1 == $result);
}
......
......@@ -43,8 +43,8 @@ public function testDeleteFeed() {
$this->assertResponse(404, 'Deleted feed source does not exist.');
// Check database for feed.
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", [':title' => $feed1->label(), ':url' => $feed1->getUrl()])->fetchField();
$this->assertFalse($result, 'Feed not found in database');
$result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed1->label())->condition('url', $feed1->getUrl())->count()->execute();
$this->assertEquals(0, $result, 'Feed not found in database');
}
}
......@@ -4,6 +4,7 @@
use Drupal\Core\Url;
use Drupal\aggregator\Entity\Feed;
use Drupal\aggregator\Entity\Item;
/**
* Tests the built-in feed parser with valid feed samples.
......@@ -57,16 +58,17 @@ public function testAtomSample() {
$this->assertText('Atom-Powered Robots Run Amok');
$this->assertLinkByHref('http://example.org/2003/12/13/atom03');
$this->assertText('Some text.');
$this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', db_query('SELECT guid FROM {aggregator_item} WHERE link = :link', [':link' => 'http://example.org/2003/12/13/atom03'])->fetchField(), 'Atom entry id element is parsed correctly.');
$iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/13/atom03')->execute();
$item = Item::load(array_values($iids)[0]);
$this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
// Check for second feed entry.
$this->assertText('We tried to stop them, but we failed.');
$this->assertLinkByHref('http://example.org/2003/12/14/atom03');
$this->assertText('Some other text.');
$db_guid = db_query('SELECT guid FROM {aggregator_item} WHERE link = :link', [
':link' => 'http://example.org/2003/12/14/atom03',
])->fetchField();
$this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $db_guid, 'Atom entry id element is parsed correctly.');
$iids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/14/atom03')->execute();
$item = Item::load(array_values($iids)[0]);
$this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
}
/**
......
......@@ -2,7 +2,7 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\Core\Database\Database;
use Drupal\aggregator\Entity\Feed;
/**
* Tests OPML import.
......@@ -46,7 +46,8 @@ public function openImportForm() {
* Submits form filled with invalid fields.
*/
public function validateImportFormFields() {
$before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$count_query = \Drupal::entityQuery('aggregator_feed')->count();
$before = $count_query->execute();
$edit = [];
$this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
......@@ -64,7 +65,7 @@ public function validateImportFormFields() {
$this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
$this->assertText(t('The URL invalidUrl://empty is not valid.'), 'Error if the URL is invalid.');
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$after = $count_query->execute();
$this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
}
......@@ -72,8 +73,8 @@ public function validateImportFormFields() {
* Submits form with invalid, empty, and valid OPML files.
*/
protected function submitImportForm() {
$connection = Database::getConnection();
$before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$count_query = \Drupal::entityQuery('aggregator_feed')->count();
$before = $count_query->execute();
$form['files[upload]'] = $this->getInvalidOpml();
$this->drupalPostForm('admin/config/services/aggregator/add/opml', $form, t('Import'));
......@@ -83,10 +84,12 @@ protected function submitImportForm() {
$this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
$this->assertText(t('No new feed has been added.'), 'Attempting to load empty OPML from remote URL.');
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$after = $count_query->execute();
$this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
$connection->delete('aggregator_feed')->execute();
foreach (Feed::loadMultiple() as $feed) {
$feed->delete();
}
$feeds[0] = $this->getFeedEditArray();
$feeds[1] = $this->getFeedEditArray();
......@@ -99,15 +102,15 @@ protected function submitImportForm() {
$this->assertRaw(t('A feed with the URL %url already exists.', ['%url' => $feeds[0]['url[0][value]']]), 'Verifying that a duplicate URL was identified');
$this->assertRaw(t('A feed named %title already exists.', ['%title' => $feeds[1]['title[0][value]']]), 'Verifying that a duplicate title was identified');
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$after = $count_query->execute();
$this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
$feeds_from_db = db_query("SELECT title, url, refresh FROM {aggregator_feed}");
$feed_entities = Feed::loadMultiple();
$refresh = TRUE;
foreach ($feeds_from_db as $feed) {
$title[$feed->url] = $feed->title;
$url[$feed->title] = $feed->url;
$refresh = $refresh && $feed->refresh == 900;
foreach ($feed_entities as $feed_entity) {
$title[$feed_entity->getUrl()] = $feed_entity->label();
$url[$feed_entity->label()] = $feed_entity->getUrl();
$refresh = $refresh && $feed_entity->getRefreshRate() == 900;
}
$this->assertEqual($title[$feeds[0]['url[0][value]']], $feeds[0]['title[0][value]'], 'First feed was added correctly.');
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Database\Database;
use Drupal\aggregator\Entity\Item;
/**
* Update feed items from a feed.
......@@ -44,27 +44,24 @@ public function testUpdateFeedItem() {
$view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'aggregator/sources/']);
$this->assert(isset($view_link), 'The message area contains a link to a feed');
$connection = Database::getConnection();
$fid = db_query("SELECT fid FROM {aggregator_feed} WHERE url = :url", [':url' => $edit['url[0][value]']])->fetchField();
$feed = Feed::load($fid);
$fids = \Drupal::entityQuery('aggregator_feed')->condition('url', $edit['url[0][value]'])->execute();
$feed = Feed::load(array_values($fids)[0]);
$feed->refreshItems();
$before = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$iids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
$before = Item::load(array_values($iids)[0])->getPostedTime();
// Sleep for 3 second.
sleep(3);
$connection->update('aggregator_feed')
->condition('fid', $feed->id())
->fields([
'checked' => 0,
'hash' => '',
'etag' => '',
'modified' => 0,
])
->execute();
$feed
->setLastCheckedTime(0)
->setHash('')
->setEtag('')
->setLastModified(0)
->save();
$feed->refreshItems();
$after = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
$after = Item::load(array_values($iids)[0])->getPostedTime();
$this->assertTrue($before === $after, format_string('Publish timestamp of feed item was not updated (@before === @after)', ['@before' => $before, '@after' => $after]));
// Make sure updating items works even after uninstalling a module
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment