Skip to content
Snippets Groups Projects
Commit 76ee2337 authored by maximpodorov's avatar maximpodorov Committed by MegaChriz
Browse files

Issue #2537926 by maximpodorov, MegaChriz, daspeter: Enhance tokens performance.

parent fa39d4cc
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,7 @@ files[] = tests/feeds_scheduler.test
files[] = tests/feeds_mapper_link.test
files[] = tests/feeds_mapper_summary.test
files[] = tests/feeds_mapper_taxonomy.test
files[] = tests/feeds_tokens.test
files[] = tests/http_request.test
files[] = tests/parser_csv.test
......
......@@ -29,19 +29,22 @@ function feeds_tokens($type, $tokens, array $data, array $options) {
$sanitize = !empty($options['sanitize']);
$feed_source_tokens = token_find_with_prefix($tokens, 'feed-source');
$feed_source_token_exists = array_key_exists('feed-source', $tokens);
if (empty($feed_source_tokens) && !$feed_source_token_exists) {
return $replacements;
}
$feed_nid = feeds_get_feed_nid($data['node']->nid, 'node');
if ($feed_nid && $feed_source = node_load($feed_nid)) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'feed-source':
$replacements[$original] = $sanitize ? check_plain($feed_source->title) : $feed_source->title;
break;
}
if (isset($tokens['feed-source'])) {
$replacements[$tokens['feed-source']] = $sanitize ? check_plain($feed_source->title) : $feed_source->title;
}
// Chained node token relationships.
if ($feed_source_tokens = token_find_with_prefix($tokens, 'feed-source')) {
if (!empty($feed_source_tokens)) {
$replacements += token_generate('node', $feed_source_tokens, array('node' => $feed_source), $options);
}
}
......
......@@ -60,6 +60,18 @@ function feeds_tests_theme() {
);
}
/**
* Implements hook_node_load().
*/
function feeds_tests_node_load($nodes) {
// Eventually keep track of nodes that get loaded.
if (variable_get('feeds_track_node_loads', FALSE)) {
$loads = variable_get('feeds_loaded_nodes', array());
$loads = array_merge($loads, array_keys($nodes));
variable_set('feeds_loaded_nodes', $loads);
}
}
/**
* Implements hook_cron_queue_alter().
*
......
<?php
/**
* Test cases for token replacement.
*/
class FeedsTokenTest extends FeedsWebTestCase {
public static function getInfo() {
return array(
'name' => 'Feeds token tests',
'description' => 'Test the Feeds tokens.',
'group' => 'Feeds',
);
}
public function setUp() {
parent::setUp();
// Create an importer configuration.
$this->createImporterConfiguration('Syndication', 'syndication');
$this->addMappings('syndication',
array(
0 => array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
)
);
}
/**
* Test if tokens defined by Feeds work.
*/
public function testFeedsTokens() {
// Import a RSS feed.
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
'title' => 'RSS Feed title',
);
$this->drupalPost('node/add/page', $edit, 'Save');
// Load an imported node.
$data = array(
'node' => node_load(2),
);
// Setup tokens to test for replacement.
$texts = array(
'Source: [node:feed-source]' => 'Source: RSS Feed title',
'Nid: [node:feed-source:nid]' => 'Nid: 1',
'Title: [node:feed-source:title]' => 'Title: RSS Feed title',
);
// Replace tokens and assert result.
foreach ($texts as $text => $expected) {
$replaced = token_replace($text, $data);
$this->assertEqual($expected, $replaced, format_string('The tokens for "@text" got replaced correctly with "@expected". Actual: "@replaced".', array(
'@text' => $text,
'@expected' => $expected,
'@replaced' => $replaced,
)));
}
}
/**
* Tests if a feed node does not get loaded if *not* replacing tokens like
* [node:feeds-source:x].
*/
public function testPerformance() {
// Import a RSS feed.
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
);
$this->drupalPost('node/add/page', $edit, 'Save');
// Keep track of loaded nodes from now on.
variable_set('feeds_track_node_loads', TRUE);
// Load an imported node.
$data = array(
'node' => node_load(2),
);
// Replace a single token.
token_replace('[node:title]', $data);
// Ensure only node 2 was loaded.
$loaded_nodes = variable_get('feeds_loaded_nodes');
$this->assertEqual(array(2), $loaded_nodes, format_string('The feed node (1) did not get loaded during token replacement, only node 2. Actual: @actual', array(
'@actual' => var_export($loaded_nodes, TRUE),
)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment