Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Class for testing "Text format" mapping configuration on text fields.
10
 */
11
class FeedsMapperFormatConfig extends FeedsMapperTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Mapper: Text format mapping configuration',
19
      'description' => 'Test text format mapping configuration for text fields.',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp() {
28
    parent::setUp(array('list', 'taxonomy'));
29
  }
30

    
31
  /**
32
   * Basic test for setting mapping configuration.
33
   */
34
  public function test() {
35
    // Create content type with three fields. Two that support text formats
36
    // and one that doesn't.
37
    $typename = $this->createContentType(array(), array(
38
      'alpha' => array(
39
        'type' => 'text',
40
        'instance_settings' => array(
41
          'instance[settings][text_processing]' => 1,
42
        ),
43
      ),
44
      'beta' => array(
45
        'type' => 'text_long',
46
        'instance_settings' => array(
47
          'instance[settings][text_processing]' => 1,
48
        ),
49
      ),
50
      'gamma' => array(
51
        'type' => 'text',
52
        'instance_settings' => array(
53
          'instance[settings][text_processing]' => 0,
54
        ),
55
      ),
56
    ));
57

    
58
    // Create a new filter format.
59
    $format = drupal_strtolower($this->randomName());
60
    $edit = array(
61
      'format' => $format,
62
      'name' => $this->randomName(),
63
      // Authenticated users.
64
      'roles[2]' => TRUE,
65
    );
66
    $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
67

    
68
    // Create and configure importer.
69
    $this->createImporterConfiguration();
70
    $this->setSettings('syndication', NULL, array('content_type' => ''));
71

    
72
    $this->setPlugin('syndication', 'FeedsFileFetcher');
73
    $this->setPlugin('syndication', 'FeedsCSVParser');
74
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
75

    
76
    $this->addMappings('syndication', array(
77
      0 => array(
78
        'source' => 'title',
79
        'target' => 'title',
80
      ),
81
      1 => array(
82
        'source' => 'created',
83
        'target' => 'created',
84
      ),
85
      2 => array(
86
        'source' => 'body',
87
        'target' => 'body',
88
        'format' => $format,
89
      ),
90
      3 => array(
91
        'source' => 'alpha',
92
        'target' => 'field_alpha',
93
        'format' => $format,
94
      ),
95
      4 => array(
96
        'source' => 'beta',
97
        'target' => 'field_beta',
98
        'format' => $format,
99
      ),
100
      5 => array(
101
        'source' => 'gamma',
102
        'target' => 'field_gamma',
103
      ),
104
    ));
105
    // Assert that for the gamma field no format can be chosen.
106
    $this->assertNoText('Text format: Plain text');
107

    
108
    // Import csv file.
109
    $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/content.csv');
110
    $this->assertText('Created 2 nodes');
111

    
112
    // Assert that fields body, alpha and beta got the expected text format.
113
    $node = node_load(1);
114
    $this->assertEqual($format, $node->body[LANGUAGE_NONE][0]['format'], 'The body field got the expected format.');
115
    $this->assertEqual($format, $node->field_alpha[LANGUAGE_NONE][0]['format'], 'The alpha field got the expected format.');
116
    $this->assertEqual($format, $node->field_beta[LANGUAGE_NONE][0]['format'], 'The beta field got the expected format.');
117
    $this->assertEqual(filter_fallback_format(), $node->field_gamma[LANGUAGE_NONE][0]['format'], 'The gama field got the expected format.');
118
  }
119

    
120
  /**
121
   * Tests the filter formats a user has access to.
122
   */
123
  public function testWithLimitedPrivileges() {
124
    // Create content type with a field that uses a text format.
125
    $typename = $this->createContentType(array(), array(
126
      'alpha' => array(
127
        'type' => 'text',
128
        'instance_settings' => array(
129
          'instance[settings][text_processing]' => 1,
130
        ),
131
      ),
132
    ));
133

    
134
    // Create a new user with limited privileges.
135
    $account = $this->drupalCreateUser(array('administer feeds'));
136

    
137
    // Create filter format the user may use.
138
    $format1 = drupal_strtolower($this->randomName());
139
    $edit1 = array(
140
      'format' => $format1,
141
      'name' => $this->randomName(),
142
      // Authenticated users.
143
      'roles[2]' => TRUE,
144
    );
145
    $this->drupalPost('admin/config/content/formats/add', $edit1, t('Save configuration'));
146

    
147
    // Create filter format the user may NOT use.
148
    $rid = $this->drupalCreateRole(array());
149
    $format2 = drupal_strtolower($this->randomName());
150
    $edit2 = array(
151
      'format' => $format2,
152
      'name' => $this->randomName(),
153
      'roles[' . $rid . ']' => TRUE,
154
    );
155
    $this->drupalPost('admin/config/content/formats/add', $edit2, t('Save configuration'));
156

    
157
    // Login as the user that may only use certain formats.
158
    $this->drupalLogin($account);
159

    
160
    // Create importer and ensure the user can use the first format.
161
    $this->createImporterConfiguration();
162
    $this->setSettings('syndication', NULL, array(
163
      'content_type' => '',
164
      'import_period' => FEEDS_SCHEDULE_NEVER,
165
    ));
166
    $this->setPlugin('syndication', 'FeedsFileFetcher');
167
    $this->setPlugin('syndication', 'FeedsCSVParser');
168
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
169
    $this->addMappings('syndication', array(
170
      0 => array(
171
        'source' => 'alpha',
172
        'target' => 'field_alpha',
173
        'format' => $format1,
174
      ),
175
    ));
176

    
177
    // Check user can choose first, but not second format as an option.
178
    $this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_0');
179
    $xpath = $this->constructFieldXpath('name', 'config[0][settings][format]');
180
    $fields = $this->xpath($xpath);
181
    $field = reset($fields);
182
    $format_options = $this->getAllOptions($field);
183
    $format1_found = FALSE;
184
    $format2_found = FALSE;
185
    foreach ($format_options as $option) {
186
      if ($option['value'] == $format1) {
187
        $format1_found = TRUE;
188
      }
189
      if ($option['value'] == $format2) {
190
        $format2_found = TRUE;
191
      }
192
    }
193
    // Assert first format can be chosen.
194
    $this->assertTrue($format1_found, format_string('Text format %format can be chosen.', array('%format' => $format1)));
195
    // Assert second format can NOT be chosen.
196
    $this->assertFalse($format2_found, format_string('Text format %format can NOT be chosen.', array('%format' => $format2)));
197
  }
198

    
199
  /**
200
   * Tests if text format can be set for taxonomy descriptions.
201
   */
202
  public function testTaxonomyDescriptionTextFormat() {
203
    // Create a vocabulary.
204
    $edit = array(
205
      'name' => 'Tags',
206
      'machine_name' => 'tags',
207
    );
208
    $this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');
209

    
210
    // Create a new filter format.
211
    $format = drupal_strtolower($this->randomName());
212
    $edit = array(
213
      'format' => $format,
214
      'name' => $this->randomName(),
215
      // Authenticated users.
216
      'roles[2]' => TRUE,
217
    );
218
    $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
219

    
220
    // Create a taxonomy term importer.
221
    $this->createImporterConfiguration();
222
    $this->setSettings('syndication', NULL, array(
223
      'content_type' => '',
224
      'import_period' => FEEDS_SCHEDULE_NEVER,
225
    ));
226
    $this->setPlugin('syndication', 'FeedsFileFetcher');
227
    $this->setPlugin('syndication', 'FeedsCSVParser');
228
    $this->setPlugin('syndication', 'FeedsTermProcessor');
229
    $this->setSettings('syndication', 'FeedsTermProcessor', array('bundle' => 'tags'));
230
    $this->addMappings('syndication', array(
231
      0 => array(
232
        'source' => 'title',
233
        'target' => 'name',
234
      ),
235
      1 => array(
236
        'source' => 'body',
237
        'target' => 'description',
238
        'format' => $format,
239
      ),
240
    ));
241

    
242
    // Import csv file.
243
    $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/content.csv');
244
    $this->assertText('Created 2 terms');
245

    
246
    // Assert that the term description got the expected text format.
247
    $term = taxonomy_term_load(1);
248
    $this->assertEqual($format, $term->format, 'The taxonomy term got the expected format.');
249
  }
250

    
251
}