Projet

Général

Profil

Paste
Télécharger (5,55 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * Tests for Rules integration.
5
 */
6
class FeedsRulesTest extends FeedsWebTestCase {
7

    
8
  public static function getInfo() {
9
    return array(
10
      'name' => 'Rules integration',
11
      'description' => 'Tests for Rules integration.',
12
      'group' => 'Feeds',
13
      'dependencies' => array('rules'),
14
    );
15
  }
16

    
17
  /**
18
   * Set up test.
19
   */
20
  public function setUp() {
21
    parent::setUp(array('rules'));
22

    
23
    // Create an importer configuration.
24
    $this->createImporterConfiguration('Node import', 'node');
25
    $this->setSettings('node', NULL, array('content_type' => ''));
26
    $this->setPlugin('node', 'FeedsHTTPFetcher');
27
    $this->setPlugin('node', 'FeedsCSVParser');
28
    $this->addMappings('node',
29
      array(
30
        0 => array(
31
          'source' => 'title',
32
          'target' => 'title',
33
          'unique' => FALSE,
34
        ),
35
        1 => array(
36
          'source' => 'guid',
37
          'target' => 'guid',
38
          'unique' => TRUE,
39
        ),
40
      )
41
    );
42

    
43
    // Clear static cache to make dynamic events available to Rules.
44
    drupal_static_reset();
45
  }
46

    
47
  /**
48
   * Creates a test rule.
49
   *
50
   * @param string $event
51
   *   The event to react on.
52
   * @param bool $action
53
   *   If a dummy action should be executed.
54
   *
55
   * @return RulesReactionRule
56
   *   An instance of RulesReactionRule.
57
   */
58
  protected function createTestRule($event, $action = TRUE) {
59
    $rule = rules_reaction_rule();
60
    $rule->event($event);
61
    if ($action) {
62
      $rule->action('feeds_tests_create_node');
63
    }
64
    return $rule;
65
  }
66

    
67
  /**
68
   * Tests if the Rules event 'feeds_before_import' is invoked.
69
   */
70
  public function testFeedsBeforeImportEvent() {
71
    $rule = $this->createTestRule('feeds_before_import');
72
    $rule->condition('data_is', array(
73
      'data:select' => 'source:id',
74
      'value' => 'node',
75
    ));
76
    $rule->integrityCheck()->save();
77

    
78
    // Set source file to import.
79
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
80
    $edit = array(
81
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
82
    );
83
    $this->drupalPost('import/node', $edit, t('Import'));
84
    $this->assertText('Created 2 nodes');
85

    
86
    // Assert that a test node was created *before* the import.
87
    $node = node_load(1);
88
    $this->assertEqual('Test node', $node->title);
89

    
90
    // Assert titles of imported nodes as well.
91
    $node = node_load(2);
92
    $this->assertEqual('Lorem ipsum', $node->title);
93
    $node = node_load(3);
94
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
95
  }
96

    
97
  /**
98
   * Tests if the Rules event 'feeds_after_import' is invoked.
99
   */
100
  public function testFeedsAfterImportEvent() {
101
    $rule = $this->createTestRule('feeds_after_import');
102
    $rule->condition('data_is', array(
103
      'data:select' => 'source:id',
104
      'value' => 'node',
105
    ));
106
    $rule->integrityCheck()->save();
107

    
108
    // Set source file to import.
109
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
110
    $edit = array(
111
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
112
    );
113
    $this->drupalPost('import/node', $edit, t('Import'));
114
    $this->assertText('Created 2 nodes');
115

    
116
    // Assert that a test node was created *after* the import.
117
    $node = node_load(3);
118
    $this->assertEqual('Test node', $node->title);
119

    
120
    // Assert titles of imported nodes as well.
121
    $node = node_load(1);
122
    $this->assertEqual('Lorem ipsum', $node->title);
123
    $node = node_load(2);
124
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
125
  }
126

    
127
  /**
128
   * Tests if the Rules event 'feeds_import_IMPORTER_ID' is invoked.
129
   */
130
  public function testFeedsBeforeSavingItemEvent() {
131
    $rule = $this->createTestRule('feeds_import_node');
132
    // Act before saving the second node.
133
    $rule->condition('data_is', array(
134
      'data:select' => 'node:title',
135
      'value' => 'Ut wisi enim ad minim veniam',
136
    ));
137
    $rule->integrityCheck()->save();
138

    
139
    // Set source file to import.
140
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
141
    $edit = array(
142
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
143
    );
144
    $this->drupalPost('import/node', $edit, t('Import'));
145
    $this->assertText('Created 2 nodes');
146

    
147
    // Assert that a test node was created before importing the second item.
148
    $node = node_load(2);
149
    $this->assertEqual('Test node', $node->title);
150

    
151
    // Assert titles of imported nodes as well.
152
    $node = node_load(1);
153
    $this->assertEqual('Lorem ipsum', $node->title);
154
    $node = node_load(3);
155
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
156
  }
157

    
158
  /**
159
   * Tests the Rules action 'feeds_skip_item'.
160
   */
161
  public function testFeedsSkipItemAction() {
162
    $rule = $this->createTestRule('feeds_import_node', FALSE);
163
    // Setup rule to not save the first item (which title is 'Lorem Ipsum').
164
    $rule->condition('data_is', array(
165
      'data:select' => 'node:title',
166
      'value' => 'Lorem ipsum',
167
    ));
168
    $rule->action('feeds_skip_item', array(
169
      'entity:select' => 'node',
170
    ));
171
    $rule->integrityCheck()->save();
172

    
173
    // Set source file to import.
174
    $source_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv';
175
    $edit = array(
176
      'feeds[FeedsHTTPFetcher][source]' => $source_url,
177
    );
178
    $this->drupalPost('import/node', $edit, t('Import'));
179
    $this->assertText('Created 1 node');
180

    
181
    // Assert that only the second item was imported.
182
    $node = node_load(1);
183
    $this->assertEqual('Ut wisi enim ad minim veniam', $node->title);
184
  }
185
}