NumberItemTest.php 3.89 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\Entity\FieldConfig;
13
use Drupal\field\Tests\FieldUnitTestBase;
14
use Drupal\field\Entity\FieldStorageConfig;
15 16 17

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

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

30
  protected function setUp() {
31 32
    parent::setUp();

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

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

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

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

}