Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for plugins/FeedsSyndicationParser.inc.
6
 */
7

    
8
/**
9
 * Test single feeds.
10
 */
11
class FeedsSyndicationParserTestCase extends FeedsMapperTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Syndication parsers',
19
      'description' => 'Regression tests for syndication parsers Common syndication and SimplePie. Tests parsers against a set of feeds in the context of Feeds module. <strong>Requires SimplePie parser to be configured correctly.</strong>',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * Run tests.
26
   */
27
  public function test() {
28
    // Only download simplepie if the plugin doesn't already exist somewhere.
29
    // People running tests locally might have it.
30
    if (!feeds_simplepie_exists()) {
31
      $this->downloadExtractSimplePie('1.3');
32
      $this->assertTrue(feeds_simplepie_exists());
33
      // Reset all the caches!
34
      $this->resetAll();
35
    }
36

    
37
    $this->createImporterConfiguration('Syndication', 'syndication');
38

    
39
    foreach (array('FeedsSyndicationParser', 'FeedsSimplePieParser') as $parser) {
40
      $this->setPlugin('syndication', $parser);
41
      foreach ($this->feedUrls() as $url => $assertions) {
42
        $this->createFeedNode('syndication', $url);
43
        $this->assertText('Created ' . $assertions['item_count'] . ' nodes');
44
      }
45
    }
46

    
47
    feeds_include_simplepie();
48
    variable_set('feeds_never_use_curl', TRUE);
49

    
50
    $link = $GLOBALS['base_url'] . '/testing/feeds/flickr.xml';
51
    $enclosure = new FeedsSimplePieEnclosure(new SimplePie_Enclosure($link));
52

    
53
    $enclosure->setAllowedExtensions('xml');
54
    $this->assertEqual(1, $enclosure->getFile('public://')->fid);
55
  }
56

    
57
  /**
58
   * Return an array of test feeds.
59
   */
60
  protected function feedUrls() {
61
    $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/';
62
    return array(
63
      "{$path}developmentseed.rss2" => array(
64
        'item_count' => 10,
65
      ),
66
      "{$path}feed_without_guid.rss2" => array(
67
        'item_count' => 10,
68
      ),
69
    );
70
  }
71

    
72
  /**
73
   * Tests if the "<source>" element of a RSS feed is parsed correctly.
74
   *
75
   * This element is optional according to the RSS 2.0 specification.
76
   */
77
  public function testRSSSourceElement() {
78
    // Do not use curl as that will result into HTTP requests returning a 404.
79
    variable_set('feeds_never_use_curl', TRUE);
80

    
81
    // Create content type with two text fields.
82
    $typename = $this->createContentType(array(), array(
83
      'source_title' => 'text',
84
      'source_url' => 'text',
85
    ));
86

    
87
    // Create importer and map sources from source element to text fields.
88
    $this->createImporterConfiguration('Syndication', 'syndication');
89
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
90
    $this->addMappings('syndication',
91
      array(
92
        0 => array(
93
          'source' => 'title',
94
          'target' => 'title',
95
          'unique' => FALSE,
96
        ),
97
        1 => array(
98
          'source' => 'source:title',
99
          'target' => 'field_source_title',
100
        ),
101
        2 => array(
102
          'source' => 'source:url',
103
          'target' => 'field_source_url',
104
        ),
105
      )
106
    );
107

    
108
    // Import url.
109
    $url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
110
    $this->createFeedNode('syndication', $url);
111

    
112
    // Assert that the contents for the source element were imported for the
113
    // first imported node.
114
    $node = node_load(2);
115
    $fields = array(
116
      'field_source_title' => array(
117
        'expected' => 'Technological Solutions for Progressive Organizations',
118
        'actual' => $node->field_source_title[LANGUAGE_NONE][0]['value'],
119
      ),
120
      'field_source_url' => array(
121
        'expected' => 'http://developmentseed.org/node/974',
122
        'actual' => $node->field_source_url[LANGUAGE_NONE][0]['value'],
123
      ),
124
    );
125
    foreach ($fields as $field_name => $value) {
126
      $this->assertEqual($value['expected'], $value['actual'], format_string('The field %field has the expected value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
127
    }
128

    
129
    // Assert that for the second imported node, no values were imported,
130
    // because the second item does not contain a source element.
131
    $node = node_load(3);
132
    foreach ($fields as $field_name => $value) {
133
      $this->assertTrue(!isset($node->{$field_name}[LANGUAGE_NONE][0]['value']), format_string('The field %field does not contain a value (actual: @actual).', array('%field' => $field_name, '@actual' => $value['actual'])));
134
    }
135
  }
136

    
137
}