Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/**
* Tests for Rules integration.
*/
class FeedsRulesTest extends FeedsWebTestCase {
public static function getInfo() {
return array(
'name' => 'Rules integration',
'description' => 'Tests for Rules integration.',
'group' => 'Feeds',
'dependencies' => array('rules'),
);
}
/**
* Set up test.
*/
public function setUp() {
parent::setUp(array('rules'));
// Create an importer configuration.
$this->createImporterConfiguration('Node import', 'node');
$this->setSettings('node', NULL, array('content_type' => ''));
$this->setPlugin('node', 'FeedsHTTPFetcher');
$this->setPlugin('node', 'FeedsCSVParser');
$this->addMappings('node',
array(
0 => array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
1 => array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
)
);
// Clear static cache to make dynamic events available to Rules.
drupal_static_reset();
}
/**
* Creates a test rule.
*
* @param string $event
* The event to react on.
* @param bool $action
* If a dummy action should be executed.
*
* @return RulesReactionRule
* An instance of RulesReactionRule.
*/
protected function createTestRule($event, $action = TRUE) {
$rule = rules_reaction_rule();
$rule->event($event);
if ($action) {
$rule->action('feeds_tests_create_node');
}
return $rule;
}
/**
* Tests if the Rules event 'feeds_before_import' is invoked.
*/
public function testFeedsBeforeImportEvent() {
$rule = $this->createTestRule('feeds_before_import');
$rule->condition('data_is', array(
'data:select' => 'source:id',
'value' => 'node',
));
$rule->integrityCheck()->save();
// Set source file to import.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $source_url,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 2 nodes');
// Assert that a test node was created *before* the import.
$node = node_load(1);
$this->assertEqual('Test node', $node->title);
// Assert titles of imported nodes as well.
$node = node_load(2);
$this->assertEqual('Lorem ipsum', $node->title);
$node = node_load(3);
$this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
}
/**
* Tests if the Rules event 'feeds_after_import' is invoked.
*/
public function testFeedsAfterImportEvent() {
$rule = $this->createTestRule('feeds_after_import');
$rule->condition('data_is', array(
'data:select' => 'source:id',
'value' => 'node',
));
$rule->integrityCheck()->save();
// Set source file to import.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $source_url,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 2 nodes');
// Assert that a test node was created *after* the import.
$node = node_load(3);
$this->assertEqual('Test node', $node->title);
// Assert titles of imported nodes as well.
$node = node_load(1);
$this->assertEqual('Lorem ipsum', $node->title);
$node = node_load(2);
$this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Tests if the Rules event 'feeds_import_IMPORTER_ID' is invoked.
*/
public function testFeedsBeforeSavingItemEvent() {
$rule = $this->createTestRule('feeds_import_node');
// Act before saving the second node.
$rule->condition('data_is', array(
'data:select' => 'node:title',
'value' => 'Ut wisi enim ad minim veniam',
));
$rule->integrityCheck()->save();
// Set source file to import.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $source_url,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 2 nodes');
// Assert that a test node was created before importing the second item.
$node = node_load(2);
$this->assertEqual('Test node', $node->title);
// Assert titles of imported nodes as well.
$node = node_load(1);
$this->assertEqual('Lorem ipsum', $node->title);
$node = node_load(3);
$this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
}
/**
* Tests the Rules action 'feeds_skip_item'.
*/
public function testFeedsSkipItemAction() {
$rule = $this->createTestRule('feeds_import_node', FALSE);
// Setup rule to not save the first item (which title is 'Lorem Ipsum').
$rule->condition('data_is', array(
'data:select' => 'node:title',
'value' => 'Lorem ipsum',
));
$rule->action('feeds_skip_item', array(
'entity:select' => 'node',
));
$rule->integrityCheck()->save();
// Set source file to import.
$source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $source_url,
);
$this->drupalPost('import/node', $edit, t('Import'));
$this->assertText('Created 1 node');
// Assert that only the second item was imported.
$node = node_load(1);
$this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
}