Projet

Général

Profil

Paste
Télécharger (7,38 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * Tests for Organic Groups integration.
10
 */
11
class FeedsOgTest extends FeedsWebTestCase {
12

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

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

    
31
    // Add OG group field to the node type 'page'.
32
    og_create_field(OG_GROUP_FIELD, 'node', 'page');
33

    
34
    // Add OG audience field to the node's "article" bundle.
35
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
36
    $og_field['field']['settings']['target_type'] = 'node';
37
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
38

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

    
42
    // Create an importer. Do not import on create.
43
    $this->createImporterConfiguration('Syndication', 'syndication');
44
    $this->setSettings('syndication', NULL, array(
45
      'import_period' => FEEDS_SCHEDULE_NEVER,
46
      'import_on_create' => FALSE,
47
    ));
48
    $this->setPlugin('syndication', 'FeedsCSVParser');
49
    $this->addMappings('syndication',
50
      array(
51
        0 => array(
52
          'source' => 'title',
53
          'target' => 'title',
54
        ),
55
      )
56
    );
57

    
58
    // Clear cache to make permission 'create article content' available.
59
    drupal_static_reset();
60
    drupal_flush_all_caches();
61
  }
62

    
63
  /**
64
   * Creates a group node.
65
   *
66
   * @param string $name
67
   *   Name of the group.
68
   * @param int $owner_uid
69
   *   User ID of the owner of the group.
70
   *
71
   * @return object
72
   *   The created group node.
73
   */
74
  protected function createGroup($name, $owner_uid) {
75
    $group = entity_create('node', array(
76
      'type' => 'page',
77
      'title' => $name,
78
      'uid' => $owner_uid,
79
    ));
80
    $wrapper = entity_metadata_wrapper('node', $group);
81
    $wrapper->{OG_GROUP_FIELD}->set(1);
82
    $wrapper->save();
83

    
84
    return $group;
85
  }
86

    
87
  /**
88
   * Sets up stuff for testing with the authorize option turned off.
89
   *
90
   * Does the following:
91
   * - Creates an account;
92
   * - Creates two group nodes;
93
   * - Adds mapping to og reference.
94
   * - Creates a feed node.
95
   *
96
   * @return object[]
97
   *   The two created group nodes.
98
   */
99
  protected function setUpStuffForTestingOgReferenceWithoutAuthorizing() {
100
    // Disable "authorize" option.
101
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('authorize' => FALSE));
102

    
103
    // Create an account who will own a group.
104
    $group_owner = $this->drupalCreateUser();
105

    
106
    // Create two groups.
107
    $group1 = $this->createGroup('Lorem', $group_owner->uid);
108
    $group2 = $this->createGroup('Ut wisi', $group_owner->uid);
109

    
110
    // Use source 'alpha' as group name.
111
    $this->addMappings('syndication',
112
      array(
113
        1 => array(
114
          'source' => 'alpha',
115
          'target' => 'og_group_ref:label',
116
        ),
117
      )
118
    );
119

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

    
128
    return array($group1, $group2);
129
  }
130

    
131
  /**
132
   * Tests importing content for a group using the web UI.
133
   */
134
  public function testImportOgReferenceWithoutAuthorizing() {
135
    list($group1, $group2) = $this->setUpStuffForTestingOgReferenceWithoutAuthorizing();
136

    
137
    // And perform import.
138
    $this->drupalPost('node/3/import', NULL, 'Import');
139
    $this->assertText('Created 2 nodes');
140

    
141
    // Assert that both nodes were created.
142
    $this->assertNodeCount(5);
143
    $node1 = node_load(4);
144
    $this->assertEqual('Lorem ipsum', $node1->title);
145
    $this->assertEqual($group1->nid, $node1->og_group_ref[LANGUAGE_NONE][0]['target_id']);
146
    $node2 = node_load(5);
147
    $this->assertEqual('Ut wisi enim ad minim veniam', $node2->title);
148
    $this->assertEqual($group2->nid, $node2->og_group_ref[LANGUAGE_NONE][0]['target_id']);
149
  }
150

    
151
  /**
152
   * Tests importing content for a group on cron runs.
153
   */
154
  public function testImportOgReferenceWithoutAuthorizingOnCron() {
155
    $this->setSettings('syndication', NULL, array(
156
      'process_in_background' => TRUE,
157
    ));
158

    
159
    list($group1, $group2) = $this->setUpStuffForTestingOgReferenceWithoutAuthorizing();
160

    
161
    // Schedule import and run cron.
162
    $this->drupalPost('node/3/import', NULL, 'Schedule import');
163
    $this->cronRun();
164

    
165
    // Assert that both nodes were created.
166
    $this->assertNodeCount(5);
167
    $node1 = node_load(4);
168
    $this->assertEqual('Lorem ipsum', $node1->title);
169
    $this->assertEqual($group1->nid, $node1->og_group_ref[LANGUAGE_NONE][0]['target_id']);
170
    $node2 = node_load(5);
171
    $this->assertEqual('Ut wisi enim ad minim veniam', $node2->title);
172
    $this->assertEqual($group2->nid, $node2->og_group_ref[LANGUAGE_NONE][0]['target_id']);
173
  }
174

    
175
  /**
176
   * Tests authorizing users importing content for a group.
177
   *
178
   * With the "authorize" option turned on, an user *must* be a member of the
179
   * group in order to import content for that group.
180
   */
181
  public function testImportOgReferenceWithAuthorizing() {
182
    // Enable "authorize" option.
183
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('authorize' => TRUE));
184

    
185
    // Use source 'alpha' as group name.
186
    $this->addMappings('syndication',
187
      array(
188
        1 => array(
189
          'source' => 'author',
190
          'target' => 'user_name',
191
        ),
192
        2 => array(
193
          'source' => 'alpha',
194
          'target' => 'og_group_ref:label',
195
        ),
196
      )
197
    );
198

    
199
    // Create a role with permission to create content.
200
    $rid = $this->drupalCreateRole(array(
201
      'access content',
202
      'create article content',
203
    ));
204

    
205
    // Create two accounts.
206
    $morticia = user_save(drupal_anonymous_user(), array(
207
      'name' => 'Morticia',
208
      'mail' => 'morticia@example.com',
209
      'pass' => 'mort',
210
      'status' => 1,
211
      'roles' => array(
212
        $rid => $rid,
213
      ),
214
    ));
215
    $fester = user_save(drupal_anonymous_user(), array(
216
      'name' => 'Fester',
217
      'mail' => 'fester@example.com',
218
      'pass' => 'fester',
219
      'status' => 1,
220
      'roles' => array(
221
        $rid => $rid,
222
      ),
223
    ));
224

    
225
    // Create two groups and make Fester the owner of both.
226
    $group1 = $this->createGroup('Lorem', $fester->uid);
227
    $group2 = $this->createGroup('Ut wisi', $fester->uid);
228

    
229
    // Create a feed node and change the author of the node.
230
    $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/content_author.csv', 'Node 1');
231
    $account = $this->drupalCreateUser(array(
232
      'access content',
233
      'create article content',
234
    ));
235
    $this->changeNodeAuthor($nid, $account);
236

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

    
240
    // Assert that only one node was imported. Only the author of the second
241
    // item is expected to be allowed to reference the group.
242
    $this->assertText('Created 1 node');
243
    $this->assertText('Failed importing 1 node');
244
    $this->assertText("Field validation errors in item 'Lorem ipsum'");
245
    $this->assertText('The referenced group (node: 1) is invalid.');
246
  }
247

    
248
}