Projet

Général

Profil

Paste
Télécharger (9,28 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * Test case for account switching.
10
 */
11
class FeedsAccountSwitcherTest extends FeedsMapperTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Feeds account switcher test',
19
      'description' => 'Tests if switching accounts during import happens properly.',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp() {
28
    parent::setUp();
29

    
30
    // Do not use curl as that will result into HTTP requests returning a 404.
31
    variable_set('feeds_never_use_curl', TRUE);
32

    
33
    // Create an importer. Do not import on create.
34
    $this->createImporterConfiguration('Syndication', 'syndication');
35
    $this->setSettings('syndication', NULL, array(
36
      'import_period' => FEEDS_SCHEDULE_NEVER,
37
      'import_on_create' => FALSE,
38
    ));
39
    $this->addMappings('syndication',
40
      array(
41
        0 => array(
42
          'source' => 'title',
43
          'target' => 'title',
44
        ),
45
      )
46
    );
47

    
48
    // Clear cache to make permission 'create article content' available.
49
    drupal_static_reset();
50
    drupal_flush_all_caches();
51
  }
52

    
53
  /**
54
   * Tests if the import is ran as the feed node author when using the UI.
55
   */
56
  public function testRunImportAsFeedNodeAuthorInUI() {
57
    // Assert that the admin user is logged in.
58
    $this->drupalGet('user');
59
    $this->clickLink('Edit');
60
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
61

    
62
    // Use the CSV parser.
63
    $this->setPlugin('syndication', 'FeedsCSVParser');
64

    
65
    // Make the feeds_tests module set the uid source to the current user.
66
    variable_set('feeds_tests_set_uid_current_user', TRUE);
67
    // And map to 'uid'.
68
    $this->addMappings('syndication',
69
      array(
70
        1 => array(
71
          'source' => 'uid',
72
          'target' => 'uid',
73
        ),
74
      )
75
    );
76

    
77
    // Create a feed node and change author of node.
78
    $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv', 'Node 1');
79
    $account = $this->drupalCreateUser(array(
80
      'access content',
81
      'create article content',
82
    ));
83
    $this->changeNodeAuthor($nid, $account);
84

    
85
    // And perform import.
86
    $this->drupalPost('node/' . $nid . '/import', NULL, 'Import');
87
    $this->assertText('Created 2 nodes');
88

    
89
    // Assert that the author of the created nodes is from the created account.
90
    $node = node_load(2);
91
    $this->assertEqual($account->uid, $node->uid, format_string('The author of the imported node is set to @expected (actual: @actual).', array(
92
      '@expected' => $account->uid,
93
      '@actual' => $node->uid,
94
    )));
95

    
96
    // Assert that the admin user is still logged in.
97
    $this->drupalGet('user');
98
    $this->clickLink('Edit');
99
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
100
  }
101

    
102
  /**
103
   * Tests if the import is ran as the feed node author when using cron.
104
   */
105
  public function testRunImportAsFeedNodeAuthorOnCron() {
106
    // Use the CSV parser.
107
    $this->setPlugin('syndication', 'FeedsCSVParser');
108
    // Run import in background.
109
    $this->setSettings('syndication', NULL, array(
110
      'process_in_background' => TRUE,
111
    ));
112

    
113
    // Make the feeds_tests module set the uid source to the current user.
114
    variable_set('feeds_tests_set_uid_current_user', TRUE);
115
    // And map to 'uid'.
116
    $this->addMappings('syndication',
117
      array(
118
        1 => array(
119
          'source' => 'uid',
120
          'target' => 'uid',
121
        ),
122
      )
123
    );
124

    
125
    // Create a feed node and change author of node.
126
    $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content.csv', 'Node 1');
127
    $account = $this->drupalCreateUser(array(
128
      'access content',
129
      'create article content',
130
    ));
131
    $this->changeNodeAuthor($nid, $account);
132

    
133
    // Schedule import and run cron.
134
    $this->drupalPost('node/' . $nid . '/import', NULL, 'Schedule import');
135
    $this->cronRun();
136

    
137
    // Assert that the author of the created nodes is from the created account.
138
    $node = node_load(2);
139
    $this->assertEqual($account->uid, $node->uid, format_string('The author of the imported node is set to @expected (actual: @actual).', array(
140
      '@expected' => $account->uid,
141
      '@actual' => $node->uid,
142
    )));
143
  }
144

    
145
  /**
146
   * Tests if an extra account switch happens on authorized imports.
147
   */
148
  public function testAuthorizedImport() {
149
    // Enable feeds_test_field module.
150
    module_enable(array('feeds_test_field'));
151

    
152
    // Create content type.
153
    $typename = $this->createContentType(array(), array(
154
      'alpha' => array(
155
        'type' => 'feeds_test_field',
156
        'widget' => 'feeds_test_field_textfield',
157
      ),
158
    ));
159

    
160
    // Create a role with permission to create content and permission to edit
161
    // fields of type 'feeds_test_field'.
162
    $rid1 = $this->drupalCreateRole(array(
163
      'access content',
164
      'create ' . $typename . ' content',
165
      'feeds_test_field.edit',
166
    ));
167
    // Create also a role that only may create content, but may NOT edit fields
168
    // of type 'feeds_test_field'.
169
    $rid2 = $this->drupalCreateRole(array(
170
      'access content',
171
      'create ' . $typename . ' content',
172
    ));
173

    
174
    // Create one account that may create content and an other
175
    // who may not.
176
    $morticia = user_save(drupal_anonymous_user(), array(
177
      'name' => 'Morticia',
178
      'mail' => 'morticia@example.com',
179
      'pass' => 'mort',
180
      'status' => 1,
181
      'roles' => array(
182
        $rid1 => $rid1,
183
      ),
184
    ));
185
    // Fester may not edit feeds_test_field fields.
186
    $fester = user_save(drupal_anonymous_user(), array(
187
      'name' => 'Fester',
188
      'mail' => 'fester@example.com',
189
      'pass' => 'fester',
190
      'status' => 1,
191
      'roles' => array(
192
        $rid2 => $rid2,
193
      ),
194
    ));
195

    
196
    // Assert that the admin user is logged in. After import, we check again
197
    // which user is logged in. It is important to ensure that it's the same
198
    // user who's logged in after import, so we can ensure that account switches
199
    // get reverted properly.
200
    $this->drupalGet('user');
201
    $this->clickLink('Edit');
202
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
203

    
204
    // Use the CSV parser.
205
    $this->setPlugin('syndication', 'FeedsCSVParser');
206
    // Turn on authorize option and set bundle.
207
    $this->setSettings('syndication', 'FeedsNodeProcessor', array(
208
      'authorize' => TRUE,
209
      'bundle' => $typename,
210
    ));
211
    // The column 'author' from the CSV contains the username.
212
    $this->addMappings('syndication',
213
      array(
214
        1 => array(
215
          'source' => 'author',
216
          'target' => 'user_name',
217
        ),
218
        2 => array(
219
          'source' => 'alpha',
220
          'target' => 'field_alpha',
221
        ),
222
      )
223
    );
224

    
225
    // Create a feed node and change the author of the node. During the import,
226
    // Feeds will initially switch to the author's account. This author is
227
    // allowed to create content, but not edit fields of type
228
    // 'feeds_test_field'. Since the import requires that permission, we can
229
    // ensure another account switch happened if the content gets imported
230
    // successfully.
231
    $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content_author.csv', 'Node 1');
232
    $account = $this->drupalCreateUser(array(
233
      'access content',
234
      'create ' . $typename . ' content',
235
    ));
236
    $this->changeNodeAuthor($nid, $account);
237

    
238
    // And perform import.
239
    $this->drupalPost('node/' . $nid . '/import', NULL, 'Import');
240

    
241
    // Assert that only one node was imported. Only the author of the first item
242
    // is expected to be allowed to edit field_alpha, but the author of the
243
    // second item isn't.
244
    $this->assertText('Created 1 node');
245
    $this->assertText('Failed importing 1 node');
246
    $this->assertText("Field validation errors in item 'Ut wisi enim ad minim veniam'");
247
    $this->assertText('You are not authorized to edit this field');
248

    
249
    // Assert that the admin user is still logged in. This ensures that after
250
    // the import the account switch has been reverted.
251
    $this->drupalGet('user');
252
    $this->clickLink('Edit');
253
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
254
  }
255

    
256
  /**
257
   * Tests if the user is switched back properly when an import fails.
258
   */
259
  public function testFailingImport() {
260
    // Set flag that causes feeds_tests_after_parse() to disrupt the import process.
261
    variable_set('feeds_tests_trigger_import_disruption', TRUE);
262

    
263
    // Assert that the admin user is logged in.
264
    $this->drupalGet('user');
265
    $this->clickLink('Edit');
266
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
267

    
268
    // Create a feed node and change author of node.
269
    $nid = $this->createFeedNode('syndication', NULL, 'Node 1');
270
    $account = $this->drupalCreateUser(array(
271
      'access content',
272
      'create article content',
273
    ));
274
    $this->changeNodeAuthor($nid, $account);
275

    
276
    // And perform import.
277
    $this->drupalPost('node/' . $nid . '/import', NULL, 'Import');
278
    $this->assertText('An error has occurred.');
279

    
280
    // Assert that the admin user is still logged in.
281
    $this->drupalGet('user');
282
    $this->clickLink('Edit');
283
    $this->assertUrl('user/' . $this->admin_user->uid . '/edit');
284
  }
285

    
286
}