NumberItemTest.php 3.83 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

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

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array();
27

28
  protected function setUp() {
29 30
    parent::setUp();

31
    // Create number field storages and fields for validation.
32
    foreach (array('integer', 'float', 'decimal') as $type) {
33
      entity_create('field_storage_config', array(
34
        'entity_type' => 'entity_test',
35
        'field_name' => 'field_' . $type,
36
        'type' => $type,
37
      ))->save();
38
      entity_create('field_config', array(
39 40 41
        'entity_type' => 'entity_test',
        'field_name' => 'field_' . $type,
        'bundle' => 'entity_test',
42
      ))->save();
43 44 45 46 47 48 49 50
    }
  }

  /**
   * Tests using entity fields of the number field type.
   */
  public function testNumberItem() {
    // Verify entity creation.
51
    $entity = entity_create('entity_test');
52 53 54 55
    $integer = rand(0, 10);
    $entity->field_integer = $integer;
    $float = 3.14;
    $entity->field_float = $float;
56 57 58
    $entity->field_decimal = '20-40';
    $violations = $entity->validate();
    $this->assertIdentical(1, count($violations), 'Wrong decimal value causes validation error');
59 60
    $decimal = '31.3';
    $entity->field_decimal = $decimal;
61
    $entity->name->value = $this->randomMachineName();
62 63 64 65 66
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
67
    $this->assertTrue($entity->field_integer instanceof FieldItemListInterface, 'Field implements interface.');
68 69 70
    $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);
71
    $this->assertTrue($entity->field_float instanceof FieldItemListInterface, 'Field implements interface.');
72 73 74
    $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);
75
    $this->assertTrue($entity->field_decimal instanceof FieldItemListInterface, 'Field implements interface.');
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    $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);
97 98 99 100 101 102 103

    /// Test sample item generation.
    $entity = entity_create('entity_test');
    $entity->field_integer->generateSampleItems();
    $entity->field_float->generateSampleItems();
    $entity->field_decimal->generateSampleItems();
    $this->entityValidateAndSave($entity);
104 105 106
  }

}