Skip to content
Snippets Groups Projects
Commit 63bed17b authored by Alex Barth's avatar Alex Barth
Browse files

#623452 mongolito404: Port basic test infrastructure for mappers, test for basic CCK mapper.

parent d7ac3fef
No related branches found
No related tags found
No related merge requests found
...@@ -148,33 +148,7 @@ class FeedsNodeProcessor extends FeedsProcessor { ...@@ -148,33 +148,7 @@ class FeedsNodeProcessor extends FeedsProcessor {
'content_type' => $type, // @todo: provide default content type feed_item. 'content_type' => $type, // @todo: provide default content type feed_item.
'update_existing' => 0, 'update_existing' => 0,
'expire' => FEEDS_EXPIRE_NEVER, 'expire' => FEEDS_EXPIRE_NEVER,
'mappings' => array( 'mappings' => array(),
'0' => array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
'1' => array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
'2' => array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
'3' => array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
'4' => array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
),
); );
} }
......
...@@ -48,7 +48,36 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { ...@@ -48,7 +48,36 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase {
public function test() { public function test() {
// Create a feed. // Create a feed.
$this->createFeedConfiguration(); $this->createFeedConfiguration('Syndication', 'syndication');
$this->addMappings('syndication',
array(
array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
)
);
$nid = $this->createFeedNode(); $nid = $this->createFeedNode();
// Assert 10 items aggregated after creation of the node. // Assert 10 items aggregated after creation of the node.
...@@ -152,6 +181,35 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { ...@@ -152,6 +181,35 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase {
'content_type' => '', 'content_type' => '',
); );
$this->drupalPost('admin/build/feeds/edit/syndication_standalone/settings', $edit, 'Save'); $this->drupalPost('admin/build/feeds/edit/syndication_standalone/settings', $edit, 'Save');
$this->addMappings('syndication_standalone',
array(
array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
)
);
// Import, assert 10 items aggregated after creation of the node. // Import, assert 10 items aggregated after creation of the node.
$this->importURL('syndication_standalone'); $this->importURL('syndication_standalone');
...@@ -514,8 +572,37 @@ class FeedsSchedulerTestCase extends FeedsWebTestCase { ...@@ -514,8 +572,37 @@ class FeedsSchedulerTestCase extends FeedsWebTestCase {
* Test scheduling on cron. * Test scheduling on cron.
*/ */
public function testScheduling() { public function testScheduling() {
// Create default configuration, turn off refresh on creation. // Create default configuration.
$this->createFeedConfiguration(); $this->createFeedConfiguration();
$this->addMappings('syndication',
array(
array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
)
);
// Create 10 feed nodes. Turn off import on create before doing that. // Create 10 feed nodes. Turn off import on create before doing that.
$edit = array( $edit = array(
......
...@@ -122,6 +122,21 @@ class FeedsWebTestCase extends DrupalWebTestCase { ...@@ -122,6 +122,21 @@ class FeedsWebTestCase extends DrupalWebTestCase {
} }
} }
/**
* Set importer or plugin settings.
*
* @param $id
* The importer configuration's id.
* @param $plugin
* The plugin (class) name, or NULL to set importer's settings
* @param $settings
* The settings to set.
*/
public function setSettings($id, $plugin, $settings) {
$this->drupalPost('admin/build/feeds/edit/'. $id .'/settings/'. $plugin, $settings, 'Save');
$this->assertText('Your changes have been saved.');
}
/** /**
* Create a test feed node. Test user has to have sufficient permissions: * Create a test feed node. Test user has to have sufficient permissions:
* *
......
"guid","title","created","alpha","beta","gamma","body"
1,"Lorem ipsum",1251936720,"Lorem",42,"4.2","Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat."
2,"Ut wisi enim ad minim veniam",1251932360,"Ut wisi",32,"1.2","Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat."
<?php
// $Id$
/**
* @file
* Test case for simple CCK field mapper mappers/content.inc.
*/
require_once(drupal_get_path('module', 'feeds') . '/tests/feeds_mapper_test.inc');
/**
* Class for testing Feeds <em>content</em> mapper.
*/
class FeedsMapperContentTestCase extends FeedsMapperTestCase {
public static function getInfo() {
return array(
'name' => t('Mapper: Content'),
'description' => t('Test Feeds Mapper support for CCK fields'),
'group' => t('Feeds'),
);
}
/**
* Set up the test.
*/
function setUp() {
// Call parent setup with required modules.
parent::setUp('feeds', 'feeds_ui', 'ctools', 'content', 'number', 'text');
// Create user and login.
$this->drupalLogin($this->drupalCreateUser(
array(
'administer content types',
'administer feeds',
'administer nodes',
'administer site configuration'
)
));
}
/**
* Basic test loading a doulbe entry CSV file.
*/
function test() {
// Create content type.
$typename = $this->createContentType(NULL, array(
'alpha' => 'text',
'beta' => 'number_integer',
'gamma' => 'number_decimal',
));
// Create and configure importer.
$this->createFeedConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array('content_type' => '','import_period' => FEEDS_SCHEDULE_NEVER,));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array('content_type' => $typename));
$this->addMappings('csv', array(
array(
'source' => 'title',
'target' => 'title',
),
array(
'source' => 'created',
'target' => 'created',
),
array(
'source' => 'body',
'target' => 'body',
),
array(
'source' => 'alpha',
'target' => 'field_alpha',
),
array(
'source' => 'beta',
'target' => 'field_beta',
),
array(
'source' => 'gamma',
'target' => 'field_gamma',
),
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() .'/tests/feeds/content.csv');
$this->assertText('Created 2 '. $typename .' nodes.');
// Check the two imported files.
$this->drupalGet('node/1/edit');
$this->assertCCKFieldValue('alpha', 'Lorem');
$this->assertCCKFieldValue('beta', '42');
$this->assertCCKFieldValue('gamma', '4.20');
$this->drupalGet('node/2/edit');
$this->assertCCKFieldValue('alpha', 'Ut wisi');
$this->assertCCKFieldValue('beta', '32');
$this->assertCCKFieldValue('gamma', '1.20');
}
}
<?php
// $Id$
/**
* @file
* Helper class with auxiliary functions for feeds mapper module tests.
*/
/**
* Base class for implementing Feeds Mapper test cases.
*/
class FeedsMapperTestCase extends FeedsWebTestCase {
// A lookup map to select the widget for each field type.
private static $field_widgets = array(
'date' => 'date_text',
'datestamp' => 'date_text',
'datetime' => 'date_text',
'number_decimal' => 'number',
'email' => 'email_textfield',
'emimage' => 'emimage_textfields',
'emaudio' => 'emaudio_textfields',
'filefield' => 'filefield_widget',
'image' => 'imagefield_widget',
'link' => 'link',
'number_float' => 'number',
'number_integer' => 'number',
'nodereference' => 'nodereference_select',
'text' => 'text_textfield',
'userreference' => 'userreference_select',
);
/**
* Assert that a form field for the given CCK field with the given value
* exists in the current form.
*
* @param $field_name
* The name of the CCK field.
* @param $value
* The (raw) value expected for the CCK field.
* @param $index
* The index of the field (for q multi-valued field).
*
* @see FeedsMapperTestCase::getFormFieldsNames()
* @see FeedsMapperTestCase::getFormFieldsValues()
*/
protected function assertCCKFieldValue($field_name, $value, $index = 0) {
$names = $this->getFormFieldsNames($field_name, $index);
$values = $this->getFormFieldsValues($field_name, $value);
foreach($names as $k => $name){
$value = $values[$k];
$this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name)));
}
}
/**
* Returns the form fields names for a given CCK field. Default implementation
* provides support for a single form field with the following name pattern
* <code>"field_{$field_name}[{$index}][value]"</code>
*
* @param $field_name
* The name of the CCK field.
* @param $index
* The index of the field (for q multi-valued field).
*
* @return
* An array of form field names.
*/
protected function getFormFieldsNames($field_name, $index) {
return array("field_{$field_name}[{$index}][value]");
}
/**
* Returns the form fields values for a given CCK field. Default implementation
* returns a single element array with $value casted to a string.
*
* @param $field_name
* The name of the CCK field.
* @param $value
* The (raw) value expected for the CCK field.
* @return An array of form field values.
*/
protected function getFormFieldsValues($field_name, $value) {
return array((string)$value);
}
/**
* Create a new content-type, and add a field to it. Mostly copied from
* cck/tests/content.crud.test ContentUICrud::testAddFieldUI
*
* @param $typename
* (Optional) the name of the content-type to create, defaults to a random name.
* @param $fields
* (Optional) an keyed array of $field_name => $field_type used to add additional
* fields to the new content type.
*
* @return
* The name of the new typename.
*/
final protected function createContentType($typename = NULL, $fields = array()) {
if ($typename == NULL) {
$typename = 'content_type_'. mt_rand();
}
// Create the content type.
$edit = array(
'type' => $typename,
'name' => $typename,
);
$this->drupalPost('admin/content/types/add', $edit, 'Save content type');
$this->assertText('The content type ' . $typename . ' has been added', 'Content type created');
$admin_type_url = 'admin/content/node-type/'. str_replace('_', '-', $typename);
// Create the fields
foreach ($fields as $field_name => $options) {
if(is_string($options)) {
$options = array('type' => $options);
}
$field_type = isset($options['type']) ? $options['type'] : 'text';
$field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
$this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
$label = $field_name . '_' . $field_type . '_label';
$edit = array(
'_add_new_field[label]' => $label,
'_add_new_field[field_name]' => $field_name,
'_add_new_field[type]' => $field_type,
'_add_new_field[widget_type]' => $field_widget,
);
$this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
// (Default) Configure the field.
$edit = isset($options['settings']) ? $options['settings'] : array();
$this->drupalPost(NULL, $edit, 'Save field settings');
$this->assertText('Added field ' . $label);
}
return $typename;
}
/**
* Select the widget for the field. Default implementation provides widgets
* for Date, Number, Text, Node reference, User reference, Email, Emfield,
* Filefield, Image, and Link.
*
* Extracted as a method to allow test implementations to add widgets for
* the tested CCK field type(s). $field_name allow to test the same
* field type with different widget (is this useful ?)
*
* @param $field_name
* The name of the field.
* @param $field_type
* The CCK type of the field.
*
* @return
* The widget for this field, or NULL if the field_type is not
* supported by this test class.
*/
protected function selectFieldWidget($field_name, $field_type) {
$field_widgets = FeedsMapperTestCase::$field_widgets;
return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment