1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains FeedsContentTypeTest.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests for when an importer is attached to a content type.
|
10
|
*/
|
11
|
class FeedsContentTypeTest extends FeedsWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* {@inheritdoc}
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => 'Feed content type',
|
19
|
'description' => 'Tests behavior for when an importer is attached to a content type.',
|
20
|
'group' => 'Feeds',
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* {@inheritdoc}
|
26
|
*/
|
27
|
public function setUp() {
|
28
|
parent::setUp();
|
29
|
|
30
|
// Create an importer configuration.
|
31
|
$this->createImporterConfiguration('Syndication', 'syndication');
|
32
|
$this->addMappings('syndication',
|
33
|
array(
|
34
|
0 => array(
|
35
|
'source' => 'title',
|
36
|
'target' => 'title',
|
37
|
'unique' => FALSE,
|
38
|
),
|
39
|
1 => array(
|
40
|
'source' => 'description',
|
41
|
'target' => 'body',
|
42
|
),
|
43
|
2 => array(
|
44
|
'source' => 'timestamp',
|
45
|
'target' => 'created',
|
46
|
),
|
47
|
3 => array(
|
48
|
'source' => 'url',
|
49
|
'target' => 'url',
|
50
|
'unique' => TRUE,
|
51
|
),
|
52
|
4 => array(
|
53
|
'source' => 'guid',
|
54
|
'target' => 'guid',
|
55
|
'unique' => TRUE,
|
56
|
),
|
57
|
)
|
58
|
);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Tests if titles can be retrieved from a feed.
|
63
|
*/
|
64
|
public function testRetrieveTitleFromFeed() {
|
65
|
// The Common syndication parser supports retrieving title from feed.
|
66
|
$edit = array(
|
67
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
|
68
|
);
|
69
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
70
|
|
71
|
$node = node_load(1);
|
72
|
$this->assertEqual('Development Seed - Technological Solutions for Progressive Organizations', $node->title, 'The title was retrieved from the feed.');
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Tests if the node title is required when the CSV parser is used.
|
77
|
*/
|
78
|
public function testRequiredNodeTitleWithCSVParser() {
|
79
|
// Set parser to CSV.
|
80
|
$this->setPlugin('syndication', 'FeedsCSVParser');
|
81
|
|
82
|
$edit = array(
|
83
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv',
|
84
|
);
|
85
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
86
|
|
87
|
$this->assertText('Title field is required.');
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Tests that the feed node gets no title if the content type does not use the
|
92
|
* node title field.
|
93
|
*/
|
94
|
public function testWithContentTypeWithoutTitle() {
|
95
|
// Set that the content type 'page' has no title.
|
96
|
db_update('node_type')
|
97
|
->fields(array(
|
98
|
'has_title' => 0,
|
99
|
))
|
100
|
->condition('type', 'page')
|
101
|
->execute();
|
102
|
|
103
|
// Flush caches so this change is picked up.
|
104
|
drupal_flush_all_caches();
|
105
|
|
106
|
// And import a RSS feed with a title.
|
107
|
$edit = array(
|
108
|
'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
|
109
|
);
|
110
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
111
|
|
112
|
// Assert that the feed node didn't got a title from the source.
|
113
|
$node = node_load(1);
|
114
|
$this->assertEqual('', $node->title, 'The feed node has no title.');
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Tests behavior when switching from standalone to attach to content type.
|
119
|
*
|
120
|
* When switching to attach to content type, the source form should be empty
|
121
|
* when adding a new feed node. Furthermore, the source that was created using
|
122
|
* the standalone form should no longer get updated on cron runs.
|
123
|
*
|
124
|
* In the end, when switching back to standalone form, the original created
|
125
|
* source should be 'restored' and updated during cron runs.
|
126
|
*/
|
127
|
public function testSwitchToAttachToContentType() {
|
128
|
// Use standalone form first.
|
129
|
// Set also to import as often as possible. This way we can test if the
|
130
|
// source gets updated on cron runs.
|
131
|
$this->setSettings('syndication', NULL, array(
|
132
|
'import_period' => 0,
|
133
|
'content_type' => '',
|
134
|
));
|
135
|
|
136
|
// Perform an import.
|
137
|
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
|
138
|
$edit = array(
|
139
|
'feeds[FeedsHTTPFetcher][source]' => $url,
|
140
|
);
|
141
|
$this->drupalPost('import/syndication', $edit, 'Import');
|
142
|
$this->assertText('Created 10 nodes');
|
143
|
|
144
|
// Delete all nodes again.
|
145
|
$this->drupalPost('import/syndication/delete-items', array(), 'Delete');
|
146
|
$this->assertText('Deleted 10 nodes');
|
147
|
|
148
|
// Ensure that no more nodes exist in the database.
|
149
|
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'No nodes exist.');
|
150
|
|
151
|
// Now switch back to attach to content type.
|
152
|
$this->setSettings('syndication', NULL, array('content_type' => 'page'));
|
153
|
|
154
|
// Go to the 'import' page.
|
155
|
$this->drupalGet('node/add/page');
|
156
|
$this->assertFieldByName('feeds[FeedsHTTPFetcher][source]', '');
|
157
|
$this->assertNoFieldByName('feeds[FeedsHTTPFetcher][source]', $url);
|
158
|
|
159
|
// Ensure that a cron task does not import content that was set using the
|
160
|
// standalone form.
|
161
|
$this->cronRun();
|
162
|
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'No nodes exist.');
|
163
|
|
164
|
// Switch back to standalone. Source should get restored.
|
165
|
$this->setSettings('syndication', NULL, array('content_type' => ''));
|
166
|
$this->drupalGet('import/syndication');
|
167
|
$this->assertFieldByName('feeds[FeedsHTTPFetcher][source]', $url);
|
168
|
|
169
|
// Run cron, nodes should get imported again.
|
170
|
$this->cronRun();
|
171
|
$this->assertEqual(10, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'Ten nodes exist.');
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Tests behavior when switching from attach to content type to standalone.
|
176
|
*
|
177
|
* When switching to a standalone form, the source form should be empty on
|
178
|
* this form. Furthermore, the source from the feed node that was created
|
179
|
* should no longer get updated on cron runs.
|
180
|
*
|
181
|
* In the end, when switching back to attach to content type, the source from
|
182
|
* the feed node should be 'restored' and updated during cron runs.
|
183
|
*/
|
184
|
public function testSwitchToStandaloneForm() {
|
185
|
// Set to import as often as possible. This way we can test if the source
|
186
|
// gets updated on cron runs.
|
187
|
$this->setSettings('syndication', NULL, array(
|
188
|
'import_period' => 0,
|
189
|
));
|
190
|
|
191
|
// Perform an import.
|
192
|
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
|
193
|
$edit = array(
|
194
|
'feeds[FeedsHTTPFetcher][source]' => $url,
|
195
|
);
|
196
|
$this->drupalPost('node/add/page', $edit, 'Save');
|
197
|
$this->assertText('Created 10 nodes');
|
198
|
|
199
|
// Delete all nodes again.
|
200
|
$this->drupalPost('node/1/delete-items', array(), 'Delete');
|
201
|
$this->assertText('Deleted 10 nodes');
|
202
|
|
203
|
// Only the feed node exist.
|
204
|
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'Only the feed node exists.');
|
205
|
|
206
|
// Now switch back to standalone form.
|
207
|
$this->setSettings('syndication', NULL, array('content_type' => ''));
|
208
|
|
209
|
// Go to 'import' page.
|
210
|
$this->drupalGet('import/syndication');
|
211
|
$this->assertFieldByName('feeds[FeedsHTTPFetcher][source]', '');
|
212
|
$this->assertNoFieldByName('feeds[FeedsHTTPFetcher][source]', $url);
|
213
|
|
214
|
// Ensure that a cron task does not import content that was set using the
|
215
|
// standalone form.
|
216
|
$this->cronRun();
|
217
|
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'Only one node exists.');
|
218
|
|
219
|
// Go to the edit page of the feed node and ensure that the feeds source
|
220
|
// form no longer exists.
|
221
|
$this->drupalGet('node/1/edit');
|
222
|
$this->assertNoFieldByName('feeds[FeedsHTTPFetcher][source]');
|
223
|
|
224
|
// Switch back to attach to content type. Sources should get restored.
|
225
|
$this->setSettings('syndication', NULL, array('content_type' => 'page'));
|
226
|
$this->drupalGet('node/1/edit');
|
227
|
$this->assertFieldByName('feeds[FeedsHTTPFetcher][source]', $url);
|
228
|
|
229
|
// Run cron, nodes should get imported again.
|
230
|
$this->cronRun();
|
231
|
$this->assertEqual(11, db_query("SELECT COUNT(*) FROM {node}")->fetchField(), 'Eleven nodes exist.');
|
232
|
}
|
233
|
|
234
|
}
|