EmailItemTest.php 2.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\email\Tests\EmailItemTest.
 */

namespace Drupal\email\Tests;

use Drupal\Core\Entity\Field\FieldInterface;
11
use Drupal\Core\Entity\Field\FieldItemInterface;
12
use Drupal\field\Tests\FieldUnitTestBase;
13 14 15 16

/**
 * Tests the new entity API for the email field type.
 */
17
class EmailItemTest extends FieldUnitTestBase {
18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('email');
25 26 27 28 29 30 31 32 33 34 35 36 37

  public static function getInfo() {
    return array(
      'name' => 'E-mail field item',
      'description' => 'Tests the new entity API for the email field type.',
      'group' => 'Field types',
    );
  }

  public function setUp() {
    parent::setUp();

    // Create an email field and instance for validation.
38
    entity_create('field_entity', array(
39 40
      'field_name' => 'field_email',
      'type' => 'email',
41 42
    ))->save();
    entity_create('field_instance', array(
43 44 45
      'entity_type' => 'entity_test',
      'field_name' => 'field_email',
      'bundle' => 'entity_test',
46
    ))->save();
47 48 49 50 51 52 53

    // Create a form display for the default form mode.
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent('field_email', array(
        'type' => 'email_default',
      ))
      ->save();
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * Tests using entity fields of the email field type.
   */
  public function testEmailItem() {
    // Verify entity creation.
    $entity = entity_create('entity_test', array());
    $value = 'test@example.com';
    $entity->field_email = $value;
    $entity->name->value = $this->randomName();
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
    $this->assertTrue($entity->field_email instanceof FieldInterface, 'Field implements interface.');
    $this->assertTrue($entity->field_email[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_email->value, $value);
    $this->assertEqual($entity->field_email[0]->value, $value);

    // Verify changing the email value.
    $new_value = $this->randomName();
    $entity->field_email->value = $new_value;
    $this->assertEqual($entity->field_email->value, $new_value);

    // Read changed entity and assert changed values.
    $entity->save();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_email->value, $new_value);
  }

}