NumberItemTest.php 3.57 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field\Tests\Number\NumberItemTest.
6 7
 */

8
namespace Drupal\field\Tests\Number;
9

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

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

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

  public static function getInfo() {
    return array(
      'name' => 'Number field items',
      'description' => 'Tests the new entity API for the number field types.',
      'group' => 'Field types',
    );
  }

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

    // Create number fields and instances for validation.
    foreach (array('integer', 'float', 'decimal') as $type) {
39
      entity_create('field_config', array(
40 41
        'name' => 'field_' . $type,
        'entity_type' => 'entity_test',
42
        'type' => $type,
43
      ))->save();
44
      entity_create('field_instance_config', array(
45 46 47
        'entity_type' => 'entity_test',
        'field_name' => 'field_' . $type,
        'bundle' => 'entity_test',
48
      ))->save();
49 50 51 52 53 54 55 56
    }
  }

  /**
   * Tests using entity fields of the number field type.
   */
  public function testNumberItem() {
    // Verify entity creation.
57
    $entity = entity_create('entity_test');
58 59 60 61 62 63 64 65 66 67 68 69
    $integer = rand(0, 10);
    $entity->field_integer = $integer;
    $float = 3.14;
    $entity->field_float = $float;
    $decimal = '31.3';
    $entity->field_decimal = $decimal;
    $entity->name->value = $this->randomName();
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
70
    $this->assertTrue($entity->field_integer instanceof FieldItemListInterface, 'Field implements interface.');
71 72 73
    $this->assertTrue($entity->field_integer[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_integer->value, $integer);
    $this->assertEqual($entity->field_integer[0]->value, $integer);
74
    $this->assertTrue($entity->field_float instanceof FieldItemListInterface, 'Field implements interface.');
75 76 77
    $this->assertTrue($entity->field_float[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_float->value, $float);
    $this->assertEqual($entity->field_float[0]->value, $float);
78
    $this->assertTrue($entity->field_decimal instanceof FieldItemListInterface, 'Field implements interface.');
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    $this->assertTrue($entity->field_decimal[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_decimal->value, $decimal);
    $this->assertEqual($entity->field_decimal[0]->value, $decimal);

    // Verify changing the number value.
    $new_integer = rand(11, 20);
    $new_float = rand(1001, 2000) / 100;
    $new_decimal = '18.2';
    $entity->field_integer->value = $new_integer;
    $this->assertEqual($entity->field_integer->value, $new_integer);
    $entity->field_float->value = $new_float;
    $this->assertEqual($entity->field_float->value, $new_float);
    $entity->field_decimal->value = $new_decimal;
    $this->assertEqual($entity->field_decimal->value, $new_decimal);

    // Read changed entity and assert changed values.
    $entity->save();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_integer->value, $new_integer);
    $this->assertEqual($entity->field_float->value, $new_float);
    $this->assertEqual($entity->field_decimal->value, $new_decimal);
  }

}