Projet

Général

Profil

Paste
Télécharger (6,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper_summary.test @ 41cc1b08

1
<?php
2

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

    
8
/**
9
 * Test case for mapping to node summary.
10
 */
11
class FeedsMapperNodeSummaryTestCase extends FeedsMapperTestCase {
12

    
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Mapper: Text with summary',
16
      'description' => 'Test Feeds Mapper support for text with summary fields.',
17
      'group' => 'Feeds',
18
    );
19
  }
20

    
21
  /**
22
   * Tests importing CSV files for text fields with summary.
23
   */
24
  public function test() {
25
    // Create and configure importer.
26
    $this->createImporterWithSummaryMapper();
27

    
28
    // Create a new filter format.
29
    $format = drupal_strtolower($this->randomName());
30
    $edit = array(
31
      'format' => $format,
32
      'name' => $this->randomName(),
33
      // Authenticated users.
34
      'roles[2]' => TRUE,
35
    );
36
    $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
37

    
38
    // The "update existing" and "skip hash check" are turned on so we can test
39
    // later if the summaries of the nodes get overwritten with the values from
40
    // the source.
41
    $this->setSettings('syndication', 'FeedsNodeProcessor', array(
42
      'update_existing' => 2,
43
      'skip_hash_check' => TRUE,
44
      'input_format' => $format,
45
    ));
46

    
47
    // Import CSV file.
48
    $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
49
    $this->assertText('Created 3 nodes');
50
    $this->assertNodeSummaryValues();
51

    
52
    // Check that text format is applied correctly.
53
    $this->drupalGet('node/1/edit');
54
    $this->assertNodeFieldValue('format', $format);
55

    
56
    // Check the teasers of the three imported nodes, assumed to be all present
57
    // on the front page.
58
    $this->assertNodeTeaserValues();
59

    
60
    // Set a summary and a text for each imported node.
61
    $edit = array(
62
      'body[und][0][summary]' => 'Nam liber tempor summary',
63
      'body[und][0][value]' => 'Nam liber tempor body',
64
    );
65
    $this->drupalPost('node/1/edit', $edit, t('Save'));
66
    $this->drupalPost('node/2/edit', $edit, t('Save'));
67
    $this->drupalPost('node/3/edit', $edit, t('Save'));
68

    
69
    // Import the same CSV file again.
70
    $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
71
    $this->assertText('Updated 3 nodes');
72
    $this->assertNodeSummaryValues();
73
    $this->assertNodeTeaserValues();
74

    
75
    // The previous texts of the nodes should no longer be visible.
76
    $this->assertNoText('Nam liber tempor summary');
77
    $this->assertNoText('Nam liber tempor body');
78

    
79
    // Check that text format is applied correctly.
80
    $this->drupalGet('node/1/edit');
81
    $this->assertNodeFieldValue('format', $format);
82
    $this->drupalGet('node/2/edit');
83
    $this->assertNodeFieldValue('format', $format);
84
    $this->drupalGet('node/3/edit');
85
    $this->assertNodeFieldValue('format', $format);
86

    
87
    // Remove the body mapping to check that the text format doesn't get updated
88
    // from the summary.
89
    $this->removeMappings('syndication', array(
90
      2 => array(
91
        'source' => 'body',
92
        'target' => 'body',
93
      ),
94
    ));
95

    
96
    // Change the text format and remove the body mapping to ensure that the
97
    // text format doesn't change.
98
    $this->setSettings('syndication', 'FeedsNodeProcessor', array(
99
      'input_format' => 'plain_text',
100
    ));
101
    $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
102
    $this->assertText('Updated 3 nodes');
103

    
104
    // Check that text format remains at its previous value.
105
    $this->drupalGet('node/1/edit');
106
    $this->assertNodeFieldValue('format', $format);
107
    $this->drupalGet('node/2/edit');
108
    $this->assertNodeFieldValue('format', $format);
109
    $this->drupalGet('node/3/edit');
110
    $this->assertNodeFieldValue('format', $format);
111
  }
112

    
113
  /**
114
   * Creates an importer with a summary mapper.
115
   *
116
   * @param $name
117
   *   The natural name of the feed.
118
   * @param $id
119
   *   The persistent id of the feed.
120
   *
121
   * @return void
122
   */
123
  protected function createImporterWithSummaryMapper($name = 'Syndication', $id = 'syndication') {
124
    // Create content type. A field named "body" which is of type "Long text and summary"
125
    // will be created by default, so we don't need to create a field of that type here.
126
    $typename = $this->createContentType(array());
127

    
128
    // Create and configure importer.
129
    $this->createImporterConfiguration($name, $id);
130
    $this->setSettings('syndication', NULL, array(
131
      'content_type' => '',
132
      'import_period' => FEEDS_SCHEDULE_NEVER,
133
    ));
134
    $this->setPlugin('syndication', 'FeedsFileFetcher');
135
    $this->setPlugin('syndication', 'FeedsCSVParser');
136
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
137
    $this->addMappings('syndication', array(
138
      0 => array(
139
        'source' => 'title',
140
        'target' => 'title',
141
      ),
142
      1 => array(
143
        'source' => 'summary',
144
        'target' => 'body:summary',
145
      ),
146
      2 => array(
147
        'source' => 'body',
148
        'target' => 'body',
149
      ),
150
      3 => array(
151
        'source' => 'guid',
152
        'target' => 'guid',
153
        'unique' => TRUE,
154
      ),
155
    ));
156
  }
157

    
158
  /**
159
   * Overrides FeedsMapperTestCase::getFormFieldsNames().
160
   *
161
   * Returns different form field names for:
162
   * - body
163
   *   This field doesn't have the "field_" prefix.
164
   * - summary
165
   *   Which is part of the body field.
166
   * - format
167
   *   The format of the body field.
168
   */
169
  protected function getFormFieldsNames($field_name, $index) {
170
    switch ($field_name) {
171
      case 'body':
172
        return array("body[und][{$index}][value]");
173

    
174
      case 'summary':
175
        return array("body[und][{$index}][summary]");
176

    
177
      case 'format':
178
        return array("body[und][{$index}][format]");
179
    }
180

    
181
    return parent::getFormFieldsNames($field_name, $index);
182
  }
183

    
184
  /**
185
   * Checks that the nodes match the imported values.
186
   */
187
  protected function assertNodeSummaryValues() {
188
    // Check the three imported nodes.
189
    $this->drupalGet('node/1/edit');
190
    $this->assertNodeFieldValue('summary', 'Lorem ipsum summary');
191
    $this->assertNodeFieldValue('body', 'Lorem ipsum body');
192
    $this->drupalGet('node/2/edit');
193
    $this->assertNodeFieldValue('summary', '');
194
    $this->assertNodeFieldValue('body', 'Ut wisi enim ad minim veniam body');
195
    $this->drupalGet('node/3/edit');
196
    $this->assertNodeFieldValue('summary', '');
197
    $this->assertNodeFieldValue('body', '');
198
  }
199

    
200
  /**
201
   * Checks the frontpage for teaser values.
202
   */
203
  protected function assertNodeTeaserValues() {
204
    $this->drupalGet('');
205
    $this->assertText('Lorem ipsum summary');
206
    $this->assertNoText('Lorem ipsum body');
207
    $this->assertText('Ut wisi enim ad minim veniam body');
208
  }
209

    
210
}