1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Test cases for token replacement.
|
5
|
*/
|
6
|
class FeedsTokenTest extends FeedsWebTestCase {
|
7
|
|
8
|
public static function getInfo() {
|
9
|
return array(
|
10
|
'name' => 'Feeds token tests',
|
11
|
'description' => 'Test the Feeds tokens.',
|
12
|
'group' => 'Feeds',
|
13
|
);
|
14
|
}
|
15
|
|
16
|
public function setUp() {
|
17
|
parent::setUp();
|
18
|
|
19
|
// Create an importer configuration.
|
20
|
$this->createImporterConfiguration('Syndication', 'syndication');
|
21
|
$this->addMappings('syndication',
|
22
|
array(
|
23
|
0 => array(
|
24
|
'source' => 'title',
|
25
|
'target' => 'title',
|
26
|
'unique' => FALSE,
|
27
|
),
|
28
|
)
|
29
|
);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Test if tokens defined by Feeds work.
|
34
|
*/
|
35
|
public function testFeedsTokens() {
|
36
|
// Import a RSS feed.
|
37
|
$edit = array(
|
38
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
|
39
|
'title' => 'RSS Feed title',
|
40
|
);
|
41
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
42
|
|
43
|
// Load an imported node.
|
44
|
$data = array(
|
45
|
'node' => node_load(2),
|
46
|
);
|
47
|
|
48
|
// Setup tokens to test for replacement.
|
49
|
$texts = array(
|
50
|
'Source: [node:feed-source]' => 'Source: RSS Feed title',
|
51
|
'Nid: [node:feed-source:nid]' => 'Nid: 1',
|
52
|
'Title: [node:feed-source:title]' => 'Title: RSS Feed title',
|
53
|
);
|
54
|
|
55
|
// Replace tokens and assert result.
|
56
|
foreach ($texts as $text => $expected) {
|
57
|
$replaced = token_replace($text, $data);
|
58
|
$this->assertEqual($expected, $replaced, format_string('The tokens for "@text" got replaced correctly with "@expected". Actual: "@replaced".', array(
|
59
|
'@text' => $text,
|
60
|
'@expected' => $expected,
|
61
|
'@replaced' => $replaced,
|
62
|
)));
|
63
|
}
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Tests if a feed node does not get loaded if *not* replacing tokens like
|
68
|
* [node:feeds-source:x].
|
69
|
*/
|
70
|
public function testPerformance() {
|
71
|
// Import a RSS feed.
|
72
|
$edit = array(
|
73
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
|
74
|
);
|
75
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
76
|
|
77
|
// Keep track of loaded nodes from now on.
|
78
|
variable_set('feeds_track_node_loads', TRUE);
|
79
|
|
80
|
// Load an imported node.
|
81
|
$data = array(
|
82
|
'node' => node_load(2),
|
83
|
);
|
84
|
|
85
|
// Replace a single token.
|
86
|
token_replace('[node:title]', $data);
|
87
|
|
88
|
// Ensure only node 2 was loaded.
|
89
|
$loaded_nodes = variable_get('feeds_loaded_nodes');
|
90
|
$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(
|
91
|
'@actual' => var_export($loaded_nodes, TRUE),
|
92
|
)));
|
93
|
}
|
94
|
}
|