1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains FeedsHooksTestCase.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests for hooks invoked by Feeds not related to mapping.
|
10
|
*/
|
11
|
class FeedsHooksTestCase extends FeedsWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* {@inheritdoc}
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => 'Hooks',
|
19
|
'description' => 'Tests for hooks invoked by Feeds not related to mapping.',
|
20
|
'group' => 'Feeds',
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* Ensure that the prevalidate hook is invoked.
|
26
|
*
|
27
|
* @see feeds_tests_feeds_prevalidate()
|
28
|
*/
|
29
|
public function testPrevalidateHook() {
|
30
|
// Include FeedsProcessor.inc to make its constants available.
|
31
|
module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
|
32
|
|
33
|
// Set flag to test prevalidate hook.
|
34
|
variable_set('feeds_tests_hook_feeds_prevalidate', TRUE);
|
35
|
|
36
|
// Create CSV importer.
|
37
|
$this->createImporterConfiguration('Content CSV', 'csv');
|
38
|
$this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
|
39
|
$this->setPlugin('csv', 'FeedsFileFetcher');
|
40
|
$this->setPlugin('csv', 'FeedsCSVParser');
|
41
|
$this->setSettings('csv', 'FeedsNodeProcessor', array(
|
42
|
'bundle' => 'article',
|
43
|
'update_existing' => FEEDS_UPDATE_EXISTING,
|
44
|
));
|
45
|
$this->addMappings('csv', array(
|
46
|
0 => array(
|
47
|
'source' => 'title',
|
48
|
'target' => 'title',
|
49
|
'unique' => TRUE,
|
50
|
),
|
51
|
));
|
52
|
|
53
|
// Create an article node that gets updated.
|
54
|
$node = $this->drupalCreateNode(array(
|
55
|
'type' => 'article',
|
56
|
'title' => 'Lorem ipsum',
|
57
|
));
|
58
|
|
59
|
// Import CSV file.
|
60
|
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
|
61
|
$this->assertText('Created 1 node');
|
62
|
$this->assertText('Updated 1 node');
|
63
|
|
64
|
$expected_result = array(
|
65
|
array(
|
66
|
'importer_id' => 'csv',
|
67
|
'title' => 'Lorem ipsum',
|
68
|
'item_guid' => 1,
|
69
|
'entity_id' => $node->nid,
|
70
|
),
|
71
|
array(
|
72
|
'importer_id' => 'csv',
|
73
|
'title' => 'Ut wisi enim ad minim veniam',
|
74
|
'item_guid' => 2,
|
75
|
'entity_id' => NULL,
|
76
|
),
|
77
|
);
|
78
|
|
79
|
// Assert that the hook was invoked. The variable should have been set in
|
80
|
// feeds_tests_feeds_prevalidate().
|
81
|
$this->assertEqual($expected_result, variable_get('feeds_tests_hook_feeds_prevalidate_results'));
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Tests the hook hook_config_defaults().
|
86
|
*/
|
87
|
public function testHookConfigDefaults() {
|
88
|
// Switch on hook implementations in the feeds_tests module.
|
89
|
variable_set('feeds_tests_hook_config_defaults', TRUE);
|
90
|
|
91
|
$this->createImporterConfiguration('Config defaults test', 'config_defaults_test');
|
92
|
|
93
|
// Load the importer and check the default value for
|
94
|
// 'feeds_tests_extra_setting'.
|
95
|
$importer = feeds_importer_load('config_defaults_test');
|
96
|
$importer_config = $importer->getConfig();
|
97
|
$this->assertFalse($importer_config['feeds_tests_extra_setting'], "Option 'Extra setting' is disabled.");
|
98
|
|
99
|
// Assert that other configurables do not have this setting.
|
100
|
$fetcher_config = $importer->fetcher->getConfig();
|
101
|
$parser_config = $importer->fetcher->getConfig();
|
102
|
$processor_config = $importer->fetcher->getConfig();
|
103
|
$this->assertFalse(isset($fetcher_config['feeds_tests_extra_setting']));
|
104
|
$this->assertFalse(isset($parser_config['feeds_tests_extra_setting']));
|
105
|
$this->assertFalse(isset($processor_config['feeds_tests_extra_setting']));
|
106
|
|
107
|
// Now change this setting.
|
108
|
$this->setSettings('config_defaults_test', NULL, array(
|
109
|
'feeds_tests_extra_setting' => TRUE,
|
110
|
));
|
111
|
|
112
|
// Reload the importer and assert that the configuration option changed.
|
113
|
drupal_static_reset();
|
114
|
$importer = feeds_importer_load('config_defaults_test');
|
115
|
$importer_config = $importer->getConfig();
|
116
|
$this->assertTrue($importer_config['feeds_tests_extra_setting'], "Option 'Extra setting' is enabled.");
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Tests the hook hook_PLUGIN_TYPE_config_defaults().
|
121
|
*/
|
122
|
public function testHookPluginTypeConfigDefaults() {
|
123
|
// Switch on hook implementations in the feeds_tests module.
|
124
|
variable_set('feeds_tests_hook_config_defaults', TRUE);
|
125
|
|
126
|
$this->createImporterConfiguration('Config defaults test', 'config_defaults_test');
|
127
|
// Change parser plugin to CSV as the common syndication parser does not
|
128
|
// have a config form.
|
129
|
$this->setPlugin('config_defaults_test', 'FeedsCSVParser');
|
130
|
|
131
|
// Load the importer and check default values for each plugin.
|
132
|
$importer = feeds_importer_load('config_defaults_test');
|
133
|
$fetcher_config = $importer->fetcher->getConfig();
|
134
|
$parser_config = $importer->parser->getConfig();
|
135
|
$processor_config = $importer->processor->getConfig();
|
136
|
$this->assertFalse($fetcher_config['feeds_tests_fetcher_extra_setting'], "Option 'Extra setting' is disabled for the fetcher.");
|
137
|
$this->assertTrue($parser_config['feeds_tests_parser_extra_setting'], "Option 'Extra setting' is enabled for the parser.");
|
138
|
$this->assertEqual('', $processor_config['feeds_tests_processor_extra_setting'], "Setting 'Extra setting' is empty.");
|
139
|
|
140
|
// Assert that the setting for a particular plugin does not exists for other
|
141
|
// configurables.
|
142
|
$importer_config = $importer->getConfig();
|
143
|
$this->assertFalse(isset($importer_config['feeds_tests_fetcher_extra_setting']));
|
144
|
$this->assertFalse(isset($importer_config['feeds_tests_parser_extra_setting']));
|
145
|
$this->assertFalse(isset($importer_config['feeds_tests_processor_extra_setting']));
|
146
|
$this->assertFalse(isset($fetcher_config['feeds_tests_parser_extra_setting']));
|
147
|
$this->assertFalse(isset($fetcher_config['feeds_tests_processor_extra_setting']));
|
148
|
$this->assertFalse(isset($parser_config['feeds_tests_fetcher_extra_setting']));
|
149
|
$this->assertFalse(isset($parser_config['feeds_tests_processor_extra_setting']));
|
150
|
$this->assertFalse(isset($processor_config['feeds_tests_fetcher_extra_setting']));
|
151
|
$this->assertFalse(isset($processor_config['feeds_tests_parser_extra_setting']));
|
152
|
|
153
|
// Now change the settings for each plugin.
|
154
|
$this->setSettings('config_defaults_test', 'FeedsHTTPFetcher', array(
|
155
|
'feeds_tests_fetcher_extra_setting' => TRUE,
|
156
|
));
|
157
|
$this->setSettings('config_defaults_test', 'FeedsCSVParser', array(
|
158
|
'feeds_tests_parser_extra_setting' => FALSE,
|
159
|
));
|
160
|
$this->setSettings('config_defaults_test', 'FeedsNodeProcessor', array(
|
161
|
'feeds_tests_processor_extra_setting' => 'my setting',
|
162
|
));
|
163
|
|
164
|
// Reload the importer and assert that the configuration changed.
|
165
|
drupal_static_reset();
|
166
|
$importer = feeds_importer_load('config_defaults_test');
|
167
|
$fetcher_config = $importer->fetcher->getConfig();
|
168
|
$parser_config = $importer->parser->getConfig();
|
169
|
$processor_config = $importer->processor->getConfig();
|
170
|
$this->assertTrue($fetcher_config['feeds_tests_fetcher_extra_setting'], "Option 'Extra setting' is enabled for the fetcher.");
|
171
|
$this->assertFalse($parser_config['feeds_tests_parser_extra_setting'], "Option 'Extra setting' is disabled for the parser.");
|
172
|
$this->assertEqual('my setting', $processor_config['feeds_tests_processor_extra_setting'], "Setting 'Extra setting' is 'my setting'.");
|
173
|
}
|
174
|
|
175
|
}
|