You are here

dynamic_entity_reference.test in Dynamic Entity Reference 7

Contains tests for the module.

File

dynamic_entity_reference.test
View source
<?php

/**
 * @file
 * Contains tests for the module.
 */

/**
 * Ensures that Dynamic Entity References field works correctly.
 */
class DynamicEntityReferenceTest extends DrupalWebTestCase {

  /**
   * Profile to use.
   */
  protected $profile = 'testing';

  /**
   * Admin user
   *
   * @var \StdClass
   */
  protected $adminUser;

  /**
   * Another user.
   *
   * @var \StdClass
   */
  protected $anotherUser;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'entityreference',
    'entity',
    'field_ui',
    'dynamic_entity_reference',
    'node',
  );
  public static function getInfo() {
    return array(
      'name' => 'Dynamic Entity Reference',
      'description' => 'Test dynamic entity reference functionality.',
      'group' => 'Dynamic Entity Reference',
    );
  }

  /**
   * Permissions to grant admin user.
   *
   * @var array
   */
  protected $permissions = array(
    'access administration pages',
    'administer content types',
    'create ponies content',
    'edit any ponies content',
    'access user profiles',
    'administer nodes',
  );

  /**
   * Sets up the test.
   */
  protected function setUp() {
    parent::setUp(self::$modules);
    $this
      ->drupalCreateContentType(array(
      'type' => 'ponies',
    ));
    $this->adminUser = $this
      ->drupalCreateUser($this->permissions);
    $this->anotherUser = $this
      ->drupalCreateUser();
  }

  /**
   * Tests field settings of dynamic entity reference field.
   */
  public function testFieldSettings() {
    $this
      ->drupalLogin($this->adminUser);

    // Add a new dynamic entity reference field.
    $this
      ->drupalGet('admin/structure/types/manage/ponies/fields');
    $edit = array(
      'fields[_add_new_field][label]' => 'Foobar',
      'fields[_add_new_field][field_name]' => 'foobar',
      'fields[_add_new_field][type]' => 'dynamic_entity_reference',
      'fields[_add_new_field][widget_type]' => 'dynamic_entity_reference_default',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalPost(NULL, array(
      'field[settings][entity_type_ids][]' => 'user',
    ), t('Save field settings'));
    $this
      ->assertFieldByName('field_foobar[und][0][target_type]');
    $this
      ->assertFieldByXPath("descendant-or-self::select[@name = 'field_foobar[und][0][target_type]']/option[@value = 'node']", 'node');
    $this
      ->assertNoFieldByXPath("descendant-or-self::select[@name = 'field_foobar[und][0][target_type]']/option[@value = 'user']", 'user');
    $this
      ->drupalPost(NULL, array(
      'field[cardinality]' => '-1',
    ), t('Save settings'));
    $this
      ->assertRaw(t('Saved %name configuration', array(
      '%name' => 'Foobar',
    )));
    $field = field_info_field('field_foobar');
    $excluded_entity_type_ids = $field['settings']['entity_type_ids'];
    $this
      ->assertNotNull($excluded_entity_type_ids);
    $this
      ->assertIdentical(array_keys($excluded_entity_type_ids), array(
      'user',
    ));

    // Check the include entity settings.
    $this
      ->drupalGet('admin/structure/types/manage/ponies/fields/field_foobar');
    $this
      ->drupalPost(NULL, array(
      'field[cardinality]' => '-1',
      'field[settings][exclude_entity_types]' => FALSE,
      'field[settings][entity_type_ids][]' => 'user',
    ), t('Save settings'));
    $this
      ->drupalGet('admin/structure/types/manage/ponies/fields/field_foobar');
    $this
      ->assertFieldByName('field_foobar[und][0][target_type]');
    $this
      ->assertFieldByXPath("descendant-or-self::select[@name = 'field_foobar[und][0][target_type]']/option[@value = 'user']", 'user');
    $this
      ->assertNoFieldByXPath("descendant-or-self::select[@name = 'field_foobar[und][0][target_type]']/option[@value = 'node']", 'node');
    $this
      ->drupalPost(NULL, array(), t('Save settings'));
    $this
      ->assertRaw(t('Saved %name configuration', array(
      '%name' => 'Foobar',
    )));
    $field = field_info_field('field_foobar');
    $excluded_entity_type_ids = $field['settings']['entity_type_ids'];
    $this
      ->assertNotNull($excluded_entity_type_ids);
    $this
      ->assertIdentical(array_keys($excluded_entity_type_ids), array(
      'user',
    ));
  }

  /**
   * Tests adding and editing values using dynamic entity reference.
   */
  public function testDynamicEntityReference() {
    $this
      ->drupalLogin($this->adminUser);

    // Add a new dynamic entity reference field.
    $this
      ->drupalGet('admin/structure/types/manage/ponies/fields');
    $edit = array(
      'fields[_add_new_field][label]' => 'Foobar',
      'fields[_add_new_field][field_name]' => 'foobar',
      'fields[_add_new_field][type]' => 'dynamic_entity_reference',
      'fields[_add_new_field][widget_type]' => 'dynamic_entity_reference_default',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalPost(NULL, array(), t('Save field settings'));
    $this
      ->drupalPost(NULL, array(
      'field[cardinality]' => '-1',
    ), t('Save settings'));
    $this
      ->assertRaw(t('Saved %name configuration', array(
      '%name' => 'Foobar',
    )));
    $this
      ->drupalPost('admin/structure/types/manage/ponies/display', array(
      'fields[field_foobar][type]' => 'dynamic_entity_reference_default',
    ), t('Save'));

    // Create some items to reference.
    $item1 = $this
      ->drupalCreateNode(array(
      'type' => 'ponies',
      'title' => 'item1',
      'status' => 1,
    ));
    $item2 = $this
      ->drupalCreateNode(array(
      'type' => 'ponies',
      'title' => 'item2',
      'status' => 1,
    ));

    // Test the new entity inherits default.
    $this
      ->drupalGet('node/add/ponies');
    $this
      ->assertField('field_foobar[und][0][target_id]', 'Found foobar field target id');
    $this
      ->assertField('field_foobar[und][0][target_type]', 'Found foobar field target type');

    // Add some extra dynamic entity reference fields.
    $this
      ->drupalPostAjax(NULL, array(), array(
      'field_foobar_add_more' => t('Add another item'),
    ), 'system/ajax', array(), array(), 'ponies-node-form');
    $this
      ->drupalPostAjax(NULL, array(), array(
      'field_foobar_add_more' => t('Add another item'),
    ), 'system/ajax', array(), array(), 'ponies-node-form');
    $edit = array(
      // Use a different user account (not the administrator) for this field
      // value so that its presence can be reliably asserted in the HTML output
      // (otherwise the assertion could pick up "Submitted by [admin]" text on
      // the node).
      'field_foobar[und][0][target_id]' => $this->anotherUser->name . ' (' . $this->anotherUser->uid . ')',
      'field_foobar[und][0][target_type]' => 'user',
      'field_foobar[und][1][target_id]' => 'item1 (' . $item1->nid . ')',
      'field_foobar[und][1][target_type]' => 'node',
      'field_foobar[und][2][target_id]' => 'item2 (' . $item2->nid . ')',
      'field_foobar[und][2][target_type]' => 'node',
      'title' => 'Barfoo',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $entities = entity_load('node', FALSE, array(
      'title' => 'Barfoo',
    ));
    $this
      ->assertEqual(1, count($entities), 'Entity was saved');
    $node = reset($entities);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('Barfoo');
    $this
      ->assertText($this->anotherUser->name);
    $this
      ->assertText('item1');
    $this
      ->assertText('item2');
    $this
      ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 3, 'Three items in field');
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][0]['target_type'], 'user');
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][0]['target_id'], $this->anotherUser->uid);
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][1]['target_type'], 'node');
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][1]['target_id'], $item1->nid);
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][2]['target_type'], 'node');
    $this
      ->assertEqual($node->field_foobar[LANGUAGE_NONE][2]['target_id'], $item2->nid);
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $edit = array(
      'title' => 'Bazbar',
      // Remove one child.
      'field_foobar[und][2][target_id]' => '',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText('Bazbar');

    // Reload entity.
    $node = node_load($node->nid, NULL, TRUE);
    $this
      ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 2, 'Two values in field');
  }

}

Classes

Namesort descending Description
DynamicEntityReferenceTest Ensures that Dynamic Entity References field works correctly.