114
tests/entityreference.admin.test
Normal file
114
tests/entityreference.admin.test
Normal file
@@ -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.'));
|
||||
}
|
||||
}
|
426
tests/entityreference.handlers.test
Normal file
426
tests/entityreference.handlers.test
Normal file
@@ -0,0 +1,426 @@
|
||||
<?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'], t('Valid result set returned by @handler.', array('@handler' => $handler_name)));
|
||||
|
||||
$result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
|
||||
$this->assertEqual($result, count($test['result']), t('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,
|
||||
),
|
||||
);
|
||||
|
||||
$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(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('published1', 'CONTAINS'),
|
||||
array('Published1', 'CONTAINS'),
|
||||
),
|
||||
'result' => array(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('published2', 'CONTAINS'),
|
||||
array('Published2', 'CONTAINS'),
|
||||
),
|
||||
'result' => 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(),
|
||||
),
|
||||
);
|
||||
$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(
|
||||
$nodes['published1']->nid => $node_labels['published1'],
|
||||
$nodes['published2']->nid => $node_labels['published2'],
|
||||
$nodes['unpublished']->nid => $node_labels['unpublished'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('Node unpublished', 'CONTAINS'),
|
||||
),
|
||||
'result' => 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(
|
||||
$users['admin']->uid => $user_labels['admin'],
|
||||
$users['non_admin']->uid => $user_labels['non_admin'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('non_admin', 'CONTAINS'),
|
||||
array('NON_ADMIN', 'CONTAINS'),
|
||||
),
|
||||
'result' => 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(
|
||||
$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(
|
||||
$users['blocked']->uid => $user_labels['blocked'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('Anonymous', 'CONTAINS'),
|
||||
array('anonymous', 'CONTAINS'),
|
||||
),
|
||||
'result' => 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(
|
||||
$comments['published_published']->cid => $comment_labels['published_published'],
|
||||
),
|
||||
),
|
||||
array(
|
||||
'arguments' => array(
|
||||
array('Published', 'CONTAINS'),
|
||||
),
|
||||
'result' => 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(
|
||||
$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(
|
||||
$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)');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user