initial commit of starter install drupal 7

This commit is contained in:
Bachir Soussi Chiadmi
2015-02-21 17:21:40 +01:00
commit eeba51cea6
6113 changed files with 1249218 additions and 0 deletions
@@ -0,0 +1,114 @@
<?php
/**
* @file
* Contains EntityReferenceHandlersTestCase
*/
/**
* Test for Entity Reference admin UI.
*/
class EntityReferenceAdminTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Entity Reference UI',
'description' => 'Tests for the administrative UI.',
'group' => 'Entity Reference',
);
}
public function setUp() {
parent::setUp(array('field_ui', 'entity', 'ctools', 'entityreference'));
// Create test user.
$this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types'));
$this->drupalLogin($this->admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
// Store a valid URL name, with hyphens instead of underscores.
$this->hyphen_type = str_replace('_', '-', $this->type);
}
protected function assertFieldSelectOptions($name, $expected_options) {
$xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
$fields = $this->xpath($xpath);
if ($fields) {
$field = $fields[0];
$options = $this->getAllOptionsList($field);
return $this->assertIdentical($options, $expected_options);
}
else {
return $this->fail(t('Unable to find field @name', array('@name' => $name)));
}
}
/**
* Extract all the options of a select element.
*/
protected function getAllOptionsList($element) {
$options = array();
// Add all options items.
foreach ($element->option as $option) {
$options[] = (string) $option['value'];
}
// TODO: support optgroup.
return $options;
}
public function testFieldAdminHandler() {
$bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;
// First step: 'Add new field' on the 'Manage fields' page.
$this->drupalPost($bundle_path . '/fields', array(
'fields[_add_new_field][label]' => 'Test label',
'fields[_add_new_field][field_name]' => 'test',
'fields[_add_new_field][type]' => 'entityreference',
'fields[_add_new_field][widget_type]' => 'entityreference_autocomplete',
), t('Save'));
// Node should be selected by default.
$this->assertFieldByName('field[settings][target_type]', 'node');
// The base handler should be selected by default.
$this->assertFieldByName('field[settings][handler]', 'base');
// The base handler settings should be diplayed.
$entity_type = 'node';
$entity_info = entity_get_info($entity_type);
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']');
}
// Test the sort settings.
$options = array('none', 'property', 'field');
$this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options);
// Option 0: no sort.
$this->assertFieldByName('field[settings][handler_settings][sort][type]', 'none');
$this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
$this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
$this->assertNoFieldByName('field[settings][handler_settings][sort][direction]');
// Option 1: sort by property.
$this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'property'), 'field[settings][handler_settings][sort][type]');
$this->assertFieldByName('field[settings][handler_settings][sort][property]', '');
$this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
$this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
// Option 2: sort by field.
$this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'field'), 'field[settings][handler_settings][sort][type]');
$this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
$this->assertFieldByName('field[settings][handler_settings][sort][field]', '');
$this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
// Set back to no sort.
$this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'none'), 'field[settings][handler_settings][sort][type]');
// Second step: 'Instance settings' form.
$this->drupalPost(NULL, array(), t('Save field settings'));
// Third step: confirm.
$this->drupalPost(NULL, array(), t('Save settings'));
// Check that the field appears in the overview form.
$this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', 'Test label', t('Field was created and appears in the overview page.'));
}
}
@@ -0,0 +1,248 @@
<?php
/**
* @file
* Test case for simple CCK field mapper mappers/content.inc.
*/
/**
* Class for testing Feeds field mapper.
*/
class FeedsMapperFieldTestCase extends DrupalWebTestCase{
/**
* Test info function.
*/
public static function getInfo() {
return array(
'name' => 'Feeds integration (field mapper)',
'description' => 'Test Feeds Mapper support for fields.',
'group' => 'Entity Reference',
);
}
/**
* Set-up function.
*/
public function setUp() {
parent::setUp();
module_enable(array('entityreference_feeds_test'), TRUE);
$this->resetAll();
if (!module_exists('feeds')) {
return;
}
$permissions[] = 'access content';
$permissions[] = 'administer site configuration';
$permissions[] = 'administer content types';
$permissions[] = 'administer nodes';
$permissions[] = 'bypass node access';
$permissions[] = 'administer taxonomy';
$permissions[] = 'administer users';
$permissions[] = 'administer feeds';
// Create an admin user and log in.
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
}
/**
* Check if mapping exists.
*
* @param string $id
* ID of the importer.
* @param integer $i
* The key of the mapping.
* @param string $source
* The source field.
* @param string $target
* The target field.
*
* @return integer
* -1 if the mapping doesn't exist, the key of the mapping otherwise.
*/
public function mappingExists($id, $i, $source, $target) {
$current_mappings = $this->getCurrentMappings($id);
if ($current_mappings) {
foreach ($current_mappings as $key => $mapping) {
if ($mapping['source'] == $source && $mapping['target'] == $target && $key == $i) {
return $key;
}
}
}
return -1;
}
/**
* Adds mappings to a given configuration.
*
* @param string $id
* ID of the importer.
* @param array $mappings
* An array of mapping arrays. Each mapping array must have a source and
* an target key and can have a unique key.
* @param bool $test_mappings
* (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
*/
public function addMappings($id, $mappings, $test_mappings = TRUE) {
$path = "admin/structure/feeds/$id/mapping";
// Iterate through all mappings and add the mapping via the form.
foreach ($mappings as $i => $mapping) {
if ($test_mappings) {
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
$this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.');
}
// Get unique flag and unset it. Otherwise, drupalPost will complain that
// Split up config and mapping.
$config = $mapping;
unset($config['source'], $config['target']);
$mapping = array('source' => $mapping['source'], 'target' => $mapping['target']);
// Add mapping.
$this->drupalPost($path, $mapping, t('Add'));
// If there are other configuration options, set them.
if ($config) {
$this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_' . $i);
// Set some settings.
$edit = array();
foreach ($config as $key => $value) {
$edit["config[$i][settings][$key]"] = $value;
}
$this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
$this->drupalPost(NULL, array(), t('Save'));
}
if ($test_mappings) {
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
$this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition.');
}
}
}
/**
* Gets an array of current mappings from the feeds_importer config.
*
* @param string $id
* ID of the importer.
*
* @return bool|array
* FALSE if the importer has no mappings, or an an array of mappings.
*/
public function getCurrentMappings($id) {
$config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField();
$config = unserialize($config);
// We are very specific here. 'mappings' can either be an array or not
// exist.
if (array_key_exists('mappings', $config['processor']['config'])) {
$this->assertTrue(is_array($config['processor']['config']['mappings']), 'Mappings is an array.');
return $config['processor']['config']['mappings'];
}
return FALSE;
}
/**
* Basic test loading a double entry CSV file.
*/
public function test() {
if (!module_exists('feeds')) {
return;
}
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/structure/types/manage/article/fields');
$this->assertText('Ref - entity ID', t('Found Entity reference field %field.', array('%field' => 'field_er_id')));
$this->assertText('Ref - entity label', t('Found Entity reference field %field.', array('%field' => 'field_er_label')));
$this->assertText('Ref - feeds GUID', t('Found Entity reference field %field.', array('%field' => 'field_er_guid')));
$this->assertText('Ref - feeds URL', t('Found Entity reference field %field.', array('%field' => 'field_er_url')));
// Add feeds importer
$this->drupalGet('admin/structure/feeds');
$this->clickLink('Add importer');
$this->drupalPost('admin/structure/feeds/create', array('name' => 'Nodes', 'id' => 'nodes'), 'Create');
$this->assertText('Your configuration has been created with default settings.');
$this->drupalPost('admin/structure/feeds/nodes/settings/', array('content_type' => '', 'import_period' => -1), 'Save');
$this->assertText('Your changes have been saved.');
$this->drupalPost("admin/structure/feeds/nodes/fetcher", array('plugin_key' => 'FeedsFileFetcher'), 'Save');
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
$this->assertEqual($config['fetcher']['plugin_key'], 'FeedsFileFetcher', 'Verified correct fetcher (FeedsFileFetcher).');
$this->drupalPost("admin/structure/feeds/nodes/parser", array('plugin_key' => 'FeedsCSVParser'), 'Save');
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
$this->assertEqual($config['parser']['plugin_key'], 'FeedsCSVParser', 'Verified correct parser (FeedsCSVParser).');
$this->drupalPost('admin/structure/feeds/nodes/settings/FeedsNodeProcessor', array('content_type' => 'article'), 'Save');
$this->assertText('Your changes have been saved.');
$this->addMappings('nodes', array(
0 => array(
'source' => 'title',
'target' => 'title',
),
1 => array(
'source' => 'nid',
'target' => 'nid',
'unique' => TRUE,
),
2 => array(
'source' => 'permalink',
'target' => 'url',
'unique' => TRUE,
),
3 => array(
'source' => 'nid',
'target' => 'guid',
'unique' => TRUE,
),
4 => array(
'source' => 'parent_nid',
'target' => 'field_er_id:etid',
),
5 => array(
'source' => 'parent_title',
'target' => 'field_er_label:label',
),
6 => array(
'source' => 'parent_url',
'target' => 'field_er_url:url',
),
7 => array(
'source' => 'parent_guid',
'target' => 'field_er_guid',
),
)
);
$file = realpath(getcwd()) . '/' . drupal_get_path('module', 'entityreference') . '/tests/feeds_test.csv';
$this->assertTrue(file_exists($file), 'Source file exists');
$this->drupalPost('import/nodes', array('files[feeds]' => $file), 'Import');
$this->assertText('Created 2 nodes');
$parent = node_load(1);
$this->assertTrue(empty($parent->field_er_id['und'][0]['target_id']), t('Parent node: Import by entity ID OK.'));
$this->assertTrue(empty($parent->field_er_label['und'][0]['target_id']), t('Parent node: Import by entity label OK.'));
$this->assertTrue(empty($parent->field_er_guid['und'][0]['target_id']), t('Parent node: Import by feeds GUID OK.'));
$this->assertTrue(empty($parent->field_er_url['und'][0]['target_id']), t('Parent node: Import by feeds URL OK.'));
$child = node_load(2);
$this->assertTrue($child->field_er_id['und'][0]['target_id'] == 1, t('Child node: Import by entity ID OK.'));
$this->assertTrue($child->field_er_label['und'][0]['target_id'] == 1, t('Child node: Import by entity label OK.'));
$this->assertTrue($child->field_er_guid['und'][0]['target_id'] == 1, t('Child node: Import by feeds GUID OK.'));
$this->assertTrue($child->field_er_url['und'][0]['target_id'] == 1, t('Child node: Import by feeds URL OK.'));
}
}
@@ -0,0 +1,597 @@
<?php
/**
* @file
* Contains EntityReferenceHandlersTestCase
*/
/**
* Test for Entity Reference handlers.
*/
class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Entity Reference Handlers',
'description' => 'Tests for the base handlers provided by Entity Reference.',
'group' => 'Entity Reference',
);
}
public function setUp() {
parent::setUp('entityreference');
}
protected function assertReferencable($field, $tests, $handler_name) {
$handler = entityreference_get_selection_handler($field);
foreach ($tests as $test) {
foreach ($test['arguments'] as $arguments) {
$result = call_user_func_array(array($handler, 'getReferencableEntities'), $arguments);
$this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', array('@handler' => $handler_name)));
$result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
if (!empty($test['result'])) {
$bundle = key($test['result']);
$count = count($test['result'][$bundle]);
}
else {
$count = 0;
}
$this->assertEqual($result, $count, format_string('Valid count returned by @handler.', array('@handler' => $handler_name)));
}
}
}
/**
* Test the node-specific overrides of the entity handler.
*/
public function testNodeHandler() {
// Build a fake field instance.
$field = array(
'translatable' => FALSE,
'entity_types' => array(),
'settings' => array(
'handler' => 'base',
'target_type' => 'node',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'test_field',
'type' => 'entityreference',
'cardinality' => '1',
);
// Build a set of test data.
// Titles contain HTML-special characters to test escaping.
$nodes = array(
'published1' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node published1 (<&>)',
'uid' => 1,
),
'published2' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node published2 (<&>)',
'uid' => 1,
),
'unpublished' => (object) array(
'type' => 'article',
'status' => 0,
'title' => 'Node unpublished (<&>)',
'uid' => 1,
),
// Title purposefully starts with same characters as published1 and
// published2 above but contains a slash.
'published_withslash' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node pub/lished1',
'uid' => 1,
),
);
$node_labels = array();
foreach ($nodes as $key => $node) {
node_save($node);
$node_labels[$key] = check_plain($node->title);
}
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content'));
$GLOBALS['user'] = $normal_user;
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
),
),
),
array(
'arguments' => array(
array('published1', 'CONTAINS'),
array('Published1', 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published1']->nid => $node_labels['published1'],
),
),
),
array(
'arguments' => array(
array('published2', 'CONTAINS'),
array('Published2', 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published2']->nid => $node_labels['published2'],
),
),
),
array(
'arguments' => array(
array('invalid node', 'CONTAINS'),
),
'result' => array(),
),
array(
'arguments' => array(
array('Node unpublished', 'CONTAINS'),
),
'result' => array(),
),
// Searching for "Node pub/" should return only the published_withslash node
// and not published1 and published2 from above.
array(
'arguments' => array(
array('Node pub/', 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'Node handler');
// Test as an admin.
$admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
$GLOBALS['user'] = $admin_user;
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['published1']->nid => $node_labels['published1'],
$nodes['published2']->nid => $node_labels['published2'],
$nodes['published_withslash']->nid => $node_labels['published_withslash'],
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
),
array(
'arguments' => array(
array('Node unpublished', 'CONTAINS'),
),
'result' => array(
'article' => array(
$nodes['unpublished']->nid => $node_labels['unpublished'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'Node handler (admin)');
}
/**
* Test the user-specific overrides of the entity handler.
*/
public function testUserHandler() {
// Build a fake field instance.
$field = array(
'translatable' => FALSE,
'entity_types' => array(),
'settings' => array(
'handler' => 'base',
'target_type' => 'user',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'test_field',
'type' => 'entityreference',
'cardinality' => '1',
);
// Build a set of test data.
$users = array(
'anonymous' => user_load(0),
'admin' => user_load(1),
'non_admin' => (object) array(
'name' => 'non_admin <&>',
'mail' => 'non_admin@example.com',
'roles' => array(),
'pass' => user_password(),
'status' => 1,
),
'blocked' => (object) array(
'name' => 'blocked <&>',
'mail' => 'blocked@example.com',
'roles' => array(),
'pass' => user_password(),
'status' => 0,
),
);
// The label of the anonymous user is variable_get('anonymous').
$users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
$user_labels = array();
foreach ($users as $key => $user) {
if (!isset($user->uid)) {
$users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
}
$user_labels[$key] = check_plain($user->name);
}
// Test as a non-admin.
$GLOBALS['user'] = $users['non_admin'];
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'user' => array(
$users['admin']->uid => '- Restricted access -',
$users['non_admin']->uid => $user_labels['non_admin'],
),
),
),
array(
'arguments' => array(
array('non_admin', 'CONTAINS'),
array('NON_ADMIN', 'CONTAINS'),
),
'result' => array(
'user' => array(
$users['non_admin']->uid => $user_labels['non_admin'],
),
),
),
array(
'arguments' => array(
array('invalid user', 'CONTAINS'),
),
'result' => array(),
),
array(
'arguments' => array(
array('blocked', 'CONTAINS'),
),
'result' => array(),
),
);
$this->assertReferencable($field, $referencable_tests, 'User handler');
$GLOBALS['user'] = $users['admin'];
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'user' => array(
$users['anonymous']->uid => $user_labels['anonymous'],
$users['admin']->uid => $user_labels['admin'],
$users['non_admin']->uid => $user_labels['non_admin'],
$users['blocked']->uid => $user_labels['blocked'],
),
),
),
array(
'arguments' => array(
array('blocked', 'CONTAINS'),
),
'result' => array(
'user' => array(
$users['blocked']->uid => $user_labels['blocked'],
),
),
),
array(
'arguments' => array(
array('Anonymous', 'CONTAINS'),
array('anonymous', 'CONTAINS'),
),
'result' => array(
'user' => array(
$users['anonymous']->uid => $user_labels['anonymous'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'User handler (admin)');
}
/**
* Test the comment-specific overrides of the entity handler.
*/
public function testCommentHandler() {
// Build a fake field instance.
$field = array(
'translatable' => FALSE,
'entity_types' => array(),
'settings' => array(
'handler' => 'base',
'target_type' => 'comment',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'test_field',
'type' => 'entityreference',
'cardinality' => '1',
);
// Build a set of test data.
$nodes = array(
'published' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node published',
'uid' => 1,
),
'unpublished' => (object) array(
'type' => 'article',
'status' => 0,
'title' => 'Node unpublished',
'uid' => 1,
),
);
foreach ($nodes as $node) {
node_save($node);
}
$comments = array(
'published_published' => (object) array(
'nid' => $nodes['published']->nid,
'uid' => 1,
'cid' => NULL,
'pid' => 0,
'status' => COMMENT_PUBLISHED,
'subject' => 'Comment Published <&>',
'hostname' => ip_address(),
'language' => LANGUAGE_NONE,
),
'published_unpublished' => (object) array(
'nid' => $nodes['published']->nid,
'uid' => 1,
'cid' => NULL,
'pid' => 0,
'status' => COMMENT_NOT_PUBLISHED,
'subject' => 'Comment Unpublished <&>',
'hostname' => ip_address(),
'language' => LANGUAGE_NONE,
),
'unpublished_published' => (object) array(
'nid' => $nodes['unpublished']->nid,
'uid' => 1,
'cid' => NULL,
'pid' => 0,
'status' => COMMENT_NOT_PUBLISHED,
'subject' => 'Comment Published on Unpublished node <&>',
'hostname' => ip_address(),
'language' => LANGUAGE_NONE,
),
);
$comment_labels = array();
foreach ($comments as $key => $comment) {
comment_save($comment);
$comment_labels[$key] = check_plain($comment->subject);
}
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
$GLOBALS['user'] = $normal_user;
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'comment_node_article' => array(
$comments['published_published']->cid => $comment_labels['published_published'],
),
),
),
array(
'arguments' => array(
array('Published', 'CONTAINS'),
),
'result' => array(
'comment_node_article' => array(
$comments['published_published']->cid => $comment_labels['published_published'],
),
),
),
array(
'arguments' => array(
array('invalid comment', 'CONTAINS'),
),
'result' => array(),
),
array(
'arguments' => array(
array('Comment Unpublished', 'CONTAINS'),
),
'result' => array(),
),
);
$this->assertReferencable($field, $referencable_tests, 'Comment handler');
// Test as a comment admin.
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
$GLOBALS['user'] = $admin_user;
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'comment_node_article' => array(
$comments['published_published']->cid => $comment_labels['published_published'],
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'Comment handler (comment admin)');
// Test as a node and comment admin.
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
$GLOBALS['user'] = $admin_user;
$referencable_tests = array(
array(
'arguments' => array(
array(NULL, 'CONTAINS'),
),
'result' => array(
'comment_node_article' => array(
$comments['published_published']->cid => $comment_labels['published_published'],
$comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
$comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
),
),
),
);
$this->assertReferencable($field, $referencable_tests, 'Comment handler (comment + node admin)');
}
/**
* Assert sorting by field works for non-admins.
*
* Since we are sorting on a field, we need to make sure the base-table
* is added, and access-control is behaving as expected.
*/
public function testSortByField() {
// Add text field to entity, to sort by.
$field_info = array(
'field_name' => 'field_text',
'type' => 'text',
'entity_types' => array('node'),
);
field_create_field($field_info);
$instance = array(
'label' => 'Text Field',
'field_name' => 'field_text',
'entity_type' => 'node',
'bundle' => 'article',
'settings' => array(),
'required' => FALSE,
);
field_create_instance($instance);
// Build a fake field instance.
$field = array(
'translatable' => FALSE,
'entity_types' => array(),
'settings' => array(
'handler' => 'base',
'target_type' => 'node',
'handler_settings' => array(
'target_bundles' => array(),
// Add sorting.
'sort' => array(
'type' => 'field',
'field' => 'field_text:value',
'direction' => 'DESC',
),
),
),
'field_name' => 'test_field',
'type' => 'entityreference',
'cardinality' => '1',
);
// Build a set of test data.
$nodes = array(
'published1' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node published1 (<&>)',
'uid' => 1,
'field_text' => array(
LANGUAGE_NONE => array(
array(
'value' => 1,
),
),
),
),
'published2' => (object) array(
'type' => 'article',
'status' => 1,
'title' => 'Node published2 (<&>)',
'uid' => 1,
'field_text' => array(
LANGUAGE_NONE => array(
array(
'value' => 2,
),
),
),
),
'unpublished' => (object) array(
'type' => 'article',
'status' => 0,
'title' => 'Node unpublished (<&>)',
'uid' => 1,
'field_text' => array(
LANGUAGE_NONE => array(
array(
'value' => 3,
),
),
),
),
);
$node_labels = array();
foreach ($nodes as $key => $node) {
node_save($node);
$node_labels[$key] = check_plain($node->title);
}
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content'));
$GLOBALS['user'] = $normal_user;
$handler = entityreference_get_selection_handler($field);
// Not only assert the result, but make sure the keys are sorted as
// expected.
$result = $handler->getReferencableEntities();
$expected_result = array(
$nodes['published2']->nid => $node_labels['published2'],
$nodes['published1']->nid => $node_labels['published1'],
);
$this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values for non-admin.');
}
}
@@ -0,0 +1,115 @@
<?php
/**
* Test for Entity Reference taxonomy integration.
*/
class EntityReferenceTaxonomyTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Entity Reference Taxonomy',
'description' => 'Tests nodes with reference to terms as indexed.',
'group' => 'Entity Reference',
);
}
public function setUp() {
parent::setUp('entityreference', 'taxonomy');
// Create an entity reference field.
$field = array(
'entity_types' => array('node'),
'settings' => array(
'handler' => 'base',
'target_type' => 'taxonomy_term',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'field_entityreference_term',
'type' => 'entityreference',
);
$field = field_create_field($field);
$instance = array(
'field_name' => 'field_entityreference_term',
'bundle' => 'article',
'entity_type' => 'node',
);
// Enable the taxonomy-index behavior.
$instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
field_create_instance($instance);
// Create a term reference field.
$field = array(
'translatable' => FALSE,
'entity_types' => array('node'),
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'terms',
'parent' => 0,
),
),
),
'field_name' => 'field_taxonomy_term',
'type' => 'taxonomy_term_reference',
);
$field = field_create_field($field);
$instance = array(
'field_name' => 'field_taxonomy_term',
'bundle' => 'article',
'entity_type' => 'node',
);
field_create_instance($instance);
// Create a terms vocobulary.
$vocabulary = new stdClass();
$vocabulary->name = 'Terms';
$vocabulary->machine_name = 'terms';
taxonomy_vocabulary_save($vocabulary);
// Create term.
for ($i = 1; $i <= 2; $i++) {
$term = new stdClass();
$term->name = "term $i";
$term->vid = 1;
taxonomy_term_save($term);
}
}
/**
* Test referencing a term using entity reference field.
*/
public function testNodeIndex() {
// Asert node insert with reference to term.
$settings = array();
$settings['type'] = 'article';
$settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = 1;
$node = $this->drupalCreateNode($settings);
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
// Asert node update with reference to term.
node_save($node);
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
// Assert node update with reference to term and taxonomy reference to
// another term.
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->field_taxonomy_term->set(2);
$wrapper->save();
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
$this->assertEqual(taxonomy_select_nodes(2), array($node->nid));
// Assert node update with reference to term and taxonomy reference to
// same term.
$wrapper->field_taxonomy_term->set(1);
$wrapper->save();
$this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
$wrapper->delete();
$this->assertFalse(taxonomy_select_nodes(1));
}
}
@@ -0,0 +1,3 @@
"nid","title","permalink","parent_nid","parent_title","parent_url","parent_guid"
"1","Views","http://drupal.org/project/views","","","",""
"2","Views bulk operations","http://drupal.org/project/views_bulk_operations","1","Views","http://drupal.org/project/views","1"
1 nid title permalink parent_nid parent_title parent_url parent_guid
2 1 Views http://drupal.org/project/views
3 2 Views bulk operations http://drupal.org/project/views_bulk_operations 1 Views http://drupal.org/project/views 1
@@ -0,0 +1,16 @@
name = "Entityreference - Feeds integration tests"
description = "Support module for the Entityreference - Feeds integration tests."
package = Testing
core = 7.x
hidden = TRUE
dependencies[] = feeds
dependencies[] = feeds_ui
dependencies[] = entityreference
; Information added by packaging script on 2013-11-20
version = "7.x-1.1"
core = "7.x"
project = "entityreference"
datestamp = "1384973110"
@@ -0,0 +1,104 @@
<?php
/**
* @file
* Support module for Entity reference - Feeds integration.
*/
/**
* Implements hook_install().
*/
function entityreference_feeds_test_install() {
field_info_cache_clear();
// Entity reference field - mapped by ID.
$field = array(
'field_name' => 'field_er_id',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_id',
'entity_type' => 'node',
'label' => 'Ref - entity ID',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Feeds GUID.
$field = array(
'field_name' => 'field_er_guid',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_guid',
'entity_type' => 'node',
'label' => 'Ref - feeds GUID',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Feeds URL.
$field = array(
'field_name' => 'field_er_url',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_url',
'entity_type' => 'node',
'label' => 'Ref - feeds URL',
'bundle' => 'article',
);
field_create_instance($instance);
// Entity reference field - mapped by Label.
$field = array(
'field_name' => 'field_er_label',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('article' => 'article'),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_er_label',
'entity_type' => 'node',
'label' => 'Ref - entity label',
'bundle' => 'article',
);
field_create_instance($instance);
}