[177a560] | 1 | <?php |
---|
| 2 | module_load_include('test', 'feeds', 'tests/feeds_mapper'); |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * Class for testing Feeds <em>content</em> mapper. |
---|
| 6 | */ |
---|
| 7 | class FeedsMapperContentTaxonomyTestCase extends FeedsMapperTestCase { |
---|
| 8 | public static function getInfo() { |
---|
| 9 | return array( |
---|
| 10 | 'name' => 'Mapper: Content Taxonomy', |
---|
| 11 | 'description' => 'Test Feeds Mapper support for Content Taxonomy CCK fields.', |
---|
| 12 | 'group' => 'Feeds', |
---|
| 13 | 'dependencies' => array('content', 'content_taxonomy'), |
---|
| 14 | ); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | public function setUp() { |
---|
| 18 | parent::setUp(array('content', 'content_taxonomy', 'content_taxonomy_autocomplete', 'content_taxonomy_options')); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * Basic test loading a single entry CSV file. |
---|
| 23 | */ |
---|
| 24 | public function test() { |
---|
| 25 | |
---|
| 26 | // Create vocabularies |
---|
| 27 | $vocabularies = array( |
---|
| 28 | 'tags' => array( |
---|
| 29 | 'name' => $this->randomName(), |
---|
| 30 | 'tags' => TRUE, |
---|
| 31 | ), |
---|
| 32 | 'categories' => array( |
---|
| 33 | 'name' => $this->randomName(), |
---|
| 34 | 'tags' => FALSE, |
---|
| 35 | ) |
---|
| 36 | ); |
---|
| 37 | foreach ($vocabularies as &$vocabulary) { |
---|
| 38 | taxonomy_save_vocabulary($vocabulary); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | // Create terms |
---|
| 42 | $terms = array( |
---|
| 43 | array( |
---|
| 44 | 'name' => 'foo', |
---|
| 45 | 'vid' => $vocabularies['tags']['vid'], |
---|
| 46 | 'weight' => 0, |
---|
| 47 | ), |
---|
| 48 | array( |
---|
| 49 | 'name' => 'lorem', |
---|
| 50 | 'vid' => $vocabularies['tags']['vid'], |
---|
| 51 | 'weight' => 0, |
---|
| 52 | ), |
---|
| 53 | array( |
---|
| 54 | 'name' => 'ipsum', |
---|
| 55 | 'vid' => $vocabularies['tags']['vid'], |
---|
| 56 | 'weight' => 0, |
---|
| 57 | ), |
---|
| 58 | array( |
---|
| 59 | 'name' => 'bar', |
---|
| 60 | 'vid' => $vocabularies['categories']['vid'], |
---|
| 61 | 'weight' => 0, |
---|
| 62 | ), |
---|
| 63 | 'consectetuer' => array( |
---|
| 64 | 'name' => 'consectetuer', |
---|
| 65 | 'vid' => $vocabularies['categories']['vid'], |
---|
| 66 | 'weight' => 0, |
---|
| 67 | ), |
---|
| 68 | ); |
---|
| 69 | foreach ($terms as &$term) { |
---|
| 70 | taxonomy_save_term($term); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | // Create content type |
---|
| 74 | $typename = $this->createContentType(array(), array( |
---|
| 75 | 'tags' => array( |
---|
| 76 | 'type' => 'content_taxonomy', |
---|
| 77 | 'widget' => 'content_taxonomy_autocomplete', |
---|
| 78 | 'settings' => array( |
---|
| 79 | 'new_terms' => 'insert', |
---|
| 80 | 'multiple' => '1', |
---|
| 81 | 'vid' => $vocabularies['tags']['vid'], |
---|
| 82 | ), |
---|
| 83 | ), |
---|
| 84 | 'categories' => array( |
---|
| 85 | 'type' => 'content_taxonomy', |
---|
| 86 | 'widget' => 'content_taxonomy_select', |
---|
| 87 | 'settings' => array( |
---|
| 88 | 'multiple' => '1', |
---|
| 89 | 'vid' => $vocabularies['categories']['vid'], |
---|
| 90 | ), |
---|
| 91 | ), |
---|
| 92 | )); |
---|
| 93 | |
---|
| 94 | // Create importer configuration |
---|
| 95 | $this->createImporterConfiguration('Content Taxonomy CSV', 'csv'); // Create a default importer configuration |
---|
| 96 | $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER)); // Importer setting |
---|
| 97 | $this->setPlugin('csv', 'FeedsFileFetcher'); //Set fetcher |
---|
| 98 | $this->setPlugin('csv', 'FeedsCSVParser'); //Set parser |
---|
| 99 | $this->setSettings('csv', 'FeedsNodeProcessor', array('content_type' => $typename)); // Processor settings |
---|
| 100 | $this->addMappings('csv', array( |
---|
| 101 | array( |
---|
| 102 | 'source' => 'title', |
---|
| 103 | 'target' => 'title' |
---|
| 104 | ), |
---|
| 105 | array( |
---|
| 106 | 'source' => 'created', |
---|
| 107 | 'target' => 'created' |
---|
| 108 | ), |
---|
| 109 | array( |
---|
| 110 | 'source' => 'body', |
---|
| 111 | 'target' => 'body' |
---|
| 112 | ), |
---|
| 113 | array( |
---|
| 114 | 'source' => 'tags', |
---|
| 115 | 'target' => 'field_tags' |
---|
| 116 | ), |
---|
| 117 | array( |
---|
| 118 | 'source' => 'categories', |
---|
| 119 | 'target' => 'field_categories' |
---|
| 120 | ), |
---|
| 121 | )); |
---|
| 122 | |
---|
| 123 | // Import CSV file. |
---|
| 124 | $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content-taxonomy.csv'); |
---|
| 125 | $this->assertText('Created 1 ' . $typename . ' node.'); |
---|
| 126 | |
---|
| 127 | // Check that the tags were stored correctly |
---|
| 128 | $this->drupalGet('node/1/edit'); |
---|
| 129 | $this->assertCCKFieldValue('tags', array('lorem', 'ipsum', 'dolor', 'sit', 'amet')); |
---|
| 130 | |
---|
| 131 | // Check that the category values were stored to the database correctly |
---|
| 132 | $this->assertEqual( |
---|
| 133 | $terms['consectetuer']['tid'], |
---|
| 134 | db_result(db_query("SELECT field_categories_value FROM {content_field_categories} WHERE nid=1")), |
---|
| 135 | t('Found expected content_taxonomy value') |
---|
| 136 | ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | protected function selectFieldWidget($field_name, $field_type) { |
---|
| 140 | if ($field_type == 'content_taxonomy') { |
---|
| 141 | return 'content_taxonomy_select'; |
---|
| 142 | } |
---|
| 143 | else { |
---|
| 144 | return parent::selectFieldWidget($field_name, $field_type); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | protected function getFormFieldsNames($field_name, $index) { |
---|
| 149 | switch ($field_name) { |
---|
| 150 | case 'tags': |
---|
| 151 | return array("field_{$field_name}[value]"); |
---|
| 152 | case 'categories': |
---|
| 153 | return array("field_{$field_name}[value][]"); |
---|
| 154 | default: |
---|
| 155 | return parent::getFormFieldsNames($field_name, $index); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | protected function getFormFieldsValues($field_name, $value) { |
---|
| 160 | switch ($field_name) { |
---|
| 161 | case 'tags': |
---|
| 162 | if (is_array($value)) { |
---|
| 163 | // @todo sort tags by weight before joining |
---|
| 164 | $value = join(', ', $value); |
---|
| 165 | } |
---|
| 166 | return array($value); |
---|
| 167 | case 'categories': |
---|
| 168 | // @todo return tid(s) from $value |
---|
| 169 | default: |
---|
| 170 | return parent::getFormFieldsValues($field_name, $value); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|