123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- /**
- * @file
- * Tests for plugins/FeedsTermProcessor.inc
- */
- /**
- * Test aggregating a feed as data records.
- */
- class FeedsCSVtoTermsTest extends FeedsWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Processor: Taxonomy',
- 'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import taxonomy terms from a CSV file.',
- 'group' => 'Feeds',
- );
- }
- /**
- * Set up test.
- */
- public function setUp() {
- parent::setUp();
- // Create an importer.
- $this->createImporterConfiguration('Term import', 'term_import');
- // Set and configure plugins and mappings.
- $this->setPlugin('term_import', 'FeedsFileFetcher');
- $this->setPlugin('term_import', 'FeedsCSVParser');
- $this->setPlugin('term_import', 'FeedsTermProcessor');
- // Create vocabulary.
- $edit = array(
- 'name' => 'Addams vocabulary',
- 'machine_name' => 'addams',
- );
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- $this->setSettings('term_import', 'FeedsTermProcessor', array('bundle' => 'addams'));
- // Use standalone form.
- $this->setSettings('term_import', NULL, array('content_type' => ''));
- }
- /**
- * Test term creation, refreshing/deleting feeds and feed items.
- */
- public function test() {
- $mappings = array(
- 0 => array(
- 'source' => 'name',
- 'target' => 'name',
- 'unique' => 1,
- ),
- );
- $this->addMappings('term_import', $mappings);
- // Import and assert.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText('Created 5 terms');
- $this->drupalGet('admin/structure/taxonomy/addams');
- $this->assertText('Morticia');
- $this->assertText('Fester');
- $this->assertText('Gomez');
- $this->assertText('Pugsley');
- // Import again.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText('There are no new terms.');
- // Force update.
- $this->setSettings('term_import', 'FeedsTermProcessor', array(
- 'skip_hash_check' => TRUE,
- 'update_existing' => 2,
- ));
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText('Updated 5 terms.');
- // Add a term manually, delete all terms, this term should still stand.
- $edit = array(
- 'name' => 'Cousin Itt',
- );
- $this->drupalPost('admin/structure/taxonomy/addams/add', $edit, t('Save'));
- $this->drupalPost('import/term_import/delete-items', array(), t('Delete'));
- $this->drupalGet('admin/structure/taxonomy/addams');
- $this->assertText('Cousin Itt');
- $this->assertNoText('Morticia');
- $this->assertNoText('Fester');
- $this->assertNoText('Gomez');
- $this->assertNoText('Pugsley');
- }
- /**
- * Test that saving an invalid vocabulary throws an exception.
- */
- public function testInvalidVocabulary() {
- $mappings = array(
- 0 => array(
- 'source' => 'name',
- 'target' => 'name',
- 'unique' => 1,
- ),
- );
- $this->addMappings('term_import', $mappings);
- // Force configuration to be invalid.
- $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'term_import'))->fetchField());
- $config['processor']['config']['bundle'] = 'does_not_exist';
- db_update('feeds_importer')
- ->fields(array('config' => serialize($config)))
- ->condition('id', 'term_import')
- ->execute();
- // Import and assert.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText(t('No vocabulary defined for Taxonomy Term processor.'));
- }
- /**
- * Tests that terms mapped to their parent by GUID are from the same vocabulary.
- */
- public function testParentTargetByGUID() {
- // Create an other vocabulary.
- $vocabulary1 = 'addams';
- $vocabulary2 = strtolower($this->randomName());
- $edit = array(
- 'name' => $this->randomString(),
- 'machine_name' => $vocabulary2,
- );
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- // Add mappings for the first importer.
- $this->addMappings('term_import',
- array(
- 0 => array(
- 'source' => 'guid',
- 'target' => 'guid',
- 'unique' => TRUE,
- ),
- 1 => array(
- 'source' => 'name',
- 'target' => 'name',
- ),
- 2 => array(
- 'source' => 'parentguid',
- 'target' => 'parentguid',
- ),
- )
- );
- // Create a second importer.
- $this->createImporterConfiguration('Term import 2', 'term_import2');
- $this->setSettings('term_import2', NULL, array('content_type' => ''));
- // Set and configure plugins and mappings.
- $this->setPlugin('term_import2', 'FeedsFileFetcher');
- $this->setPlugin('term_import2', 'FeedsCSVParser');
- $this->setPlugin('term_import2', 'FeedsTermProcessor');
- $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
- // Add mappings for the second importer.
- $this->addMappings('term_import2',
- array(
- 0 => array(
- 'source' => 'guid',
- 'target' => 'guid',
- 'unique' => TRUE,
- ),
- 1 => array(
- 'source' => 'name',
- 'target' => 'name',
- ),
- 2 => array(
- 'source' => 'parentguid',
- 'target' => 'parentguid',
- ),
- )
- );
- $values = array(
- 1 => 'Europe',
- 2 => 'Belgium',
- );
- // Import file using the first importer.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
- $this->assertText('Created 2 terms.');
- // Assert that two terms were created in the first vocabulary.
- $terms = entity_load('taxonomy_term', array_keys($values));
- foreach ($terms as $tid => $term) {
- $this->assertEqual($values[$tid], $term->name);
- $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
- }
- // Assert that the second term's parent is the first term.
- $parents = taxonomy_get_parents($terms[2]->tid);
- $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
- if (!empty($parents)) {
- $parent = current($parents);
- $this->assertEqual(1, $parent->tid, $message);
- }
- else {
- $this->fail($message);
- }
- $values = array(
- 3 => 'Europe',
- 4 => 'Belgium',
- );
- // Now import the file using the second importer.
- $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
- $this->assertText('Created 2 terms.');
- // Assert that two terms were created in the second vocabulary.
- $terms = entity_load('taxonomy_term', array_keys($values));
- foreach ($terms as $tid => $term) {
- $this->assertEqual($values[$tid], $term->name);
- $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
- }
- // Assert that the second term's parent is the first term.
- $parents = taxonomy_get_parents($terms[4]->tid);
- $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
- if (!empty($parents)) {
- $parent = current($parents);
- $this->assertEqual(3, $parent->tid, $message);
- }
- else {
- $this->fail($message);
- }
- }
- /**
- * Tests that terms mapped to their parent by GUID are from the same vocabulary.
- */
- public function testParentTargetByName() {
- // Create an other vocabulary.
- $vocabulary1 = 'addams';
- $vocabulary2 = strtolower($this->randomName());
- $edit = array(
- 'name' => $this->randomString(),
- 'machine_name' => $vocabulary2,
- );
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- // Add mappings for the first importer.
- $this->addMappings('term_import',
- array(
- 0 => array(
- 'source' => 'guid',
- 'target' => 'guid',
- 'unique' => TRUE,
- ),
- 1 => array(
- 'source' => 'name',
- 'target' => 'name',
- ),
- 2 => array(
- 'source' => 'parent',
- 'target' => 'parent',
- ),
- )
- );
- // Create a second importer.
- $this->createImporterConfiguration('Term import 2', 'term_import2');
- $this->setSettings('term_import2', NULL, array('content_type' => ''));
- // Set and configure plugins and mappings.
- $this->setPlugin('term_import2', 'FeedsFileFetcher');
- $this->setPlugin('term_import2', 'FeedsCSVParser');
- $this->setPlugin('term_import2', 'FeedsTermProcessor');
- $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
- // Add mappings for the second importer.
- $this->addMappings('term_import2',
- array(
- 0 => array(
- 'source' => 'guid',
- 'target' => 'guid',
- 'unique' => TRUE,
- ),
- 1 => array(
- 'source' => 'name',
- 'target' => 'name',
- ),
- 2 => array(
- 'source' => 'parent',
- 'target' => 'parent',
- ),
- )
- );
- $values = array(
- 1 => 'Europe',
- 2 => 'Belgium',
- );
- // Import file using the first importer.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
- $this->assertText('Created 2 terms.');
- // Assert that two terms were created in the first vocabulary.
- $terms = entity_load('taxonomy_term', array_keys($values));
- foreach ($terms as $tid => $term) {
- $this->assertEqual($values[$tid], $term->name);
- $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
- }
- // Assert that the second term's parent is the first term.
- $parents = taxonomy_get_parents($terms[2]->tid);
- $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
- if (!empty($parents)) {
- $parent = current($parents);
- $this->assertEqual(1, $parent->tid, $message);
- }
- else {
- $this->fail($message);
- }
- $values = array(
- 3 => 'Europe',
- 4 => 'Belgium',
- );
- // Now import the file using the second importer.
- $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
- $this->assertText('Created 2 terms.');
- // Assert that two terms were created in the second vocabulary.
- $terms = entity_load('taxonomy_term', array_keys($values));
- foreach ($terms as $tid => $term) {
- $this->assertEqual($values[$tid], $term->name);
- $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
- }
- // Assert that the second term's parent is the first term.
- $parents = taxonomy_get_parents($terms[4]->tid);
- $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
- if (!empty($parents)) {
- $parent = current($parents);
- $this->assertEqual(3, $parent->tid, $message);
- }
- else {
- $this->fail($message);
- }
- }
- /**
- * Test replacing terms on subsequent imports.
- */
- public function testReplaceTerms() {
- $mappings = array(
- 0 => array(
- 'source' => 'name',
- 'target' => 'name',
- 'unique' => 1,
- ),
- );
- $this->addMappings('term_import', $mappings);
- // Configure the processor to "Replace existing terms".
- $this->setSettings('term_import', 'FeedsTermProcessor', array(
- 'skip_hash_check' => TRUE,
- 'update_existing' => 1,
- ));
- // Import first time.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText('Created 5 terms');
- // Import again to replace terms.
- $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
- $this->assertText('Updated 5 terms.');
- }
- }
|