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