Projet

Général

Profil

Paste
Télécharger (8,77 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_rules.test @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsRulesTest.
6
 */
7

    
8
/**
9
 * Tests for Rules integration.
10
 */
11
class FeedsRulesTest extends FeedsWebTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Rules integration',
19
      'description' => 'Tests for Rules integration.',
20
      'group' => 'Feeds',
21
      'dependencies' => array('rules'),
22
    );
23
  }
24

    
25
  /**
26
   * Set up test.
27
   */
28
  public function setUp() {
29
    parent::setUp(array('rules'));
30

    
31
    // Create an importer configuration.
32
    $this->createImporterConfiguration('Node import', 'node');
33
    $this->setSettings('node', NULL, array('content_type' => ''));
34
    $this->setPlugin('node', 'FeedsHTTPFetcher');
35
    $this->setPlugin('node', 'FeedsCSVParser');
36
    $this->addMappings('node',
37
      array(
38
        0 => array(
39
          'source' => 'title',
40
          'target' => 'title',
41
          'unique' => FALSE,
42
        ),
43
        1 => array(
44
          'source' => 'guid',
45
          'target' => 'guid',
46
          'unique' => TRUE,
47
        ),
48
      )
49
    );
50

    
51
    // Clear static cache to make dynamic events available to Rules.
52
    drupal_static_reset();
53
  }
54

    
55
  /**
56
   * Creates a test rule.
57
   *
58
   * @param string $event
59
   *   The event to react on.
60
   * @param bool $action
61
   *   If a dummy action should be executed.
62
   *
63
   * @return RulesReactionRule
64
   *   An instance of RulesReactionRule.
65
   */
66
  protected function createTestRule($event, $action = TRUE) {
67
    $rule = rules_reaction_rule();
68
    $rule->event($event);
69
    if ($action) {
70
      $rule->action('feeds_tests_create_node');
71
    }
72
    return $rule;
73
  }
74

    
75
  /**
76
   * Tests if the Rules event 'feeds_before_import' is invoked.
77
   */
78
  public function testFeedsBeforeImportEvent() {
79
    $rule = $this->createTestRule('feeds_before_import');
80
    $rule->condition('data_is', array(
81
      'data:select' => 'source:id',
82
      'value' => 'node',
83
    ));
84
    $rule->integrityCheck()->save();
85

    
86
    // Set source file to import.
87
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
88
    $edit = array(
89
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
90
    );
91
    $this->drupalPost('import/node', $edit, t('Import'));
92
    $this->assertText('Created 2 nodes');
93

    
94
    // Assert that a test node was created *before* the import.
95
    $node = node_load(1);
96
    $this->assertEqual('Test node', $node->title);
97

    
98
    // Assert titles of imported nodes as well.
99
    $node = node_load(2);
100
    $this->assertEqual('Lorem ipsum', $node->title);
101
    $node = node_load(3);
102
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
103
  }
104

    
105
  /**
106
   * Tests if the Rules event 'feeds_after_import' is invoked.
107
   */
108
  public function testFeedsAfterImportEvent() {
109
    $rule = $this->createTestRule('feeds_after_import');
110
    $rule->condition('data_is', array(
111
      'data:select' => 'source:id',
112
      'value' => 'node',
113
    ));
114
    $rule->integrityCheck()->save();
115

    
116
    // Set source file to import.
117
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
118
    $edit = array(
119
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
120
    );
121
    $this->drupalPost('import/node', $edit, t('Import'));
122
    $this->assertText('Created 2 nodes');
123

    
124
    // Assert that a test node was created *after* the import.
125
    $node = node_load(3);
126
    $this->assertEqual('Test node', $node->title);
127

    
128
    // Assert titles of imported nodes as well.
129
    $node = node_load(1);
130
    $this->assertEqual('Lorem ipsum', $node->title);
131
    $node = node_load(2);
132
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
133
  }
134

    
135
  /**
136
   * Tests if the Rules event 'feeds_import_IMPORTER_ID' is invoked.
137
   */
138
  public function testFeedsBeforeSavingItemEvent() {
139
    $rule = $this->createTestRule('feeds_import_node');
140
    // Act before saving the second node.
141
    $rule->condition('data_is', array(
142
      'data:select' => 'node:title',
143
      'value' => 'Ut wisi enim ad minim veniam',
144
    ));
145
    $rule->integrityCheck()->save();
146

    
147
    // Set source file to import.
148
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
149
    $edit = array(
150
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
151
    );
152
    $this->drupalPost('import/node', $edit, t('Import'));
153
    $this->assertText('Created 2 nodes');
154

    
155
    // Assert that a test node was created before importing the second item.
156
    $node = node_load(2);
157
    $this->assertEqual('Test node', $node->title);
158

    
159
    // Assert titles of imported nodes as well.
160
    $node = node_load(1);
161
    $this->assertEqual('Lorem ipsum', $node->title);
162
    $node = node_load(3);
163
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
164
  }
165

    
166
  /**
167
   * Tests the Rules action 'feeds_skip_item'.
168
   */
169
  public function testFeedsSkipItemAction() {
170
    $rule = $this->createTestRule('feeds_import_node', FALSE);
171
    // Setup rule to not save the first item (which title is 'Lorem Ipsum').
172
    $rule->condition('data_is', array(
173
      'data:select' => 'node:title',
174
      'value' => 'Lorem ipsum',
175
    ));
176
    $rule->action('feeds_skip_item', array(
177
      'entity:select' => 'node',
178
    ));
179
    $rule->integrityCheck()->save();
180

    
181
    // Set source file to import.
182
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
183
    $edit = array(
184
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
185
    );
186
    $this->drupalPost('import/node', $edit, t('Import'));
187
    $this->assertText('Created 1 node');
188

    
189
    // Assert that only the second item was imported.
190
    $node = node_load(1);
191
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
192
  }
193

    
194
  /**
195
   * Tests the Rules action 'feeds_import_feed'.
196
   */
197
  public function testFeedsImportAction() {
198
    // Attach importer to content type and set to not import on submission.
199
    $this->setSettings('node', NULL, array(
200
      'content_type' => 'page',
201
      'import_period' => FEEDS_SCHEDULE_NEVER,
202
      'import_on_create' => FALSE,
203
    ));
204

    
205
    // Create a feed node.
206
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
207
    $this->createFeedNode('node', $source_url, 'Feed node 1', 'page');
208

    
209
    // Assert that nothing has been imported yet.
210
    $this->assertNodeCount(1);
211

    
212
    // Create rule with import action.
213
    $rule = $this->createTestRule('cron', FALSE);
214
    $rule->action('feeds_import_feed', array(
215
      'importer' => 'node',
216
      'feed_nid' => 1,
217
    ));
218
    $rule->integrityCheck()->save();
219

    
220
    // Trigger rules event.
221
    $this->cronRun();
222

    
223
    // Assert that 2 items have been imported (three nodes exist in total).
224
    $this->assertNodeCount(3);
225
  }
226

    
227
  /**
228
   * Tests the Rules action 'feeds_import_feed' with standalone form.
229
   */
230
  public function testFeedsImportActionUsingStandaloneForm() {
231
    $this->setSettings('node', NULL, array(
232
      'import_period' => FEEDS_SCHEDULE_NEVER,
233
      'import_on_create' => FALSE,
234
    ));
235

    
236
    // Save data on import form.
237
    $edit = array(
238
      'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv',
239
    );
240
    $this->drupalPost('import/node', $edit, 'Save');
241

    
242
    // Create rule with import action.
243
    $rule = $this->createTestRule('cron', FALSE);
244
    $rule->action('feeds_import_feed', array(
245
      'importer' => 'node',
246
      'feed_nid' => 0,
247
    ));
248
    $rule->integrityCheck()->save();
249

    
250
    // Trigger rules event.
251
    $this->cronRun();
252

    
253
    // Assert that 2 items have been imported.
254
    $this->assertNodeCount(2);
255
  }
256

    
257
  /**
258
   * Tests the Rules action 'feeds_import_feed' with process in background.
259
   */
260
  public function testFeedsImportActionWithProcessInBackgroundOption() {
261
    // Attach importer to content type, set to not import on submission and set
262
    // to run in background.
263
    $this->setSettings('node', NULL, array(
264
      'content_type' => 'page',
265
      'import_period' => FEEDS_SCHEDULE_NEVER,
266
      'import_on_create' => FALSE,
267
      'process_in_background' => TRUE,
268
    ));
269

    
270
    // Create a feed node.
271
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
272
    $this->createFeedNode('node', $source_url, 'Feed node 1', 'page');
273

    
274
    // Assert that nothing has been imported yet.
275
    $this->assertNodeCount(1);
276

    
277
    // Create rule with import action.
278
    $rule = $this->createTestRule('feeds_tests_rules_event', FALSE);
279
    $rule->action('feeds_import_feed', array(
280
      'importer' => 'node',
281
      'feed_nid' => 1,
282
    ));
283
    $rule->integrityCheck()->save();
284

    
285
    // Trigger rules event.
286
    $this->drupalGet('testing/feeds/trigger-rules-event');
287

    
288
    // Run cron to run background task.
289
    $this->cronRun();
290

    
291
    // Assert that 2 items have been imported (three nodes exist in total).
292
    $this->assertNodeCount(3);
293
  }
294

    
295
}