FieldDataCountTest.php 6.22 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\field\Tests\FieldDataCountTest.
6
7
8
9
 */

namespace Drupal\field\Tests;

10
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
11
12

/**
13
14
 * Tests counting field data records and the hasData() method on
 * FieldStorageConfig entity.
15
 *
16
 * @group field
17
 * @see \Drupal\Core\Entity\FieldableEntityStorageInterface::countFieldData()
18
 * @see \Drupal\field\Entity\FieldStorageConfig::hasData()
19
 */
20
class FieldDataCountTest extends FieldUnitTestBase {
21

22
  /**
23
   * @var \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface
24
25
26
   */
  protected $storage;

27
28
29
  /**
   * @var \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface
   */
30
  protected $storageRev;
31

32
33
34
35
36
  /**
   * @var \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface
   */
  protected $storageUser;

37
38
39
  /**
   * {@inheritdoc}
   */
40
  protected function setUp() {
41
    parent::setUp();
42
    $this->installEntitySchema('entity_test_rev');
43
    $this->storage = \Drupal::entityManager()->getStorage('entity_test');
44
    $this->storageRev = \Drupal::entityManager()->getStorage('entity_test_rev');
45
    $this->storageUser = \Drupal::entityManager()->getStorage('user');
46
47
  }

48
49
50
51
52
53
  /**
   * Tests entityCount() and hadData() methods.
   */
  public function testEntityCountAndHasData() {
    // Create a field with a cardinality of 2 to show that we are counting
    // entities and not rows in a table.
54
55
    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = entity_create('field_storage_config', array(
56
      'field_name' => 'field_int',
57
58
59
60
      'entity_type' => 'entity_test',
      'type' => 'integer',
      'cardinality' => 2,
    ));
61
    $field_storage->save();
62
    entity_create('field_config', array(
63
      'field_storage' => $field_storage,
64
65
66
      'bundle' => 'entity_test',
    ))->save();

67
68
    $this->assertIdentical($field_storage->hasdata(), FALSE, 'There are no entities with field data.');
    $this->assertIdentical($this->storage->countFieldData($field_storage), 0, 'There are 0 entities with field data.');
69
70
71

    // Create 1 entity without the field.
    $entity = entity_create('entity_test');
72
    $entity->name->value = $this->randomMachineName();
73
74
    $entity->save();

75
76
    $this->assertIdentical($field_storage->hasdata(), FALSE, 'There are no entities with field data.');
    $this->assertIdentical($this->storage->countFieldData($field_storage), 0, 'There are 0 entities with field data.');
77
78
79
80

    // Create 12 entities to ensure that the purging works as expected.
    for ($i=0; $i < 12; $i++) {
      $entity = entity_create('entity_test');
81
82
83
      $entity->field_int[] = mt_rand(1,99);
      $entity->field_int[] = mt_rand(1,99);
      $entity->name[] = $this->randomMachineName();
84
85
86
87
      $entity->save();
    }

    $storage = \Drupal::entityManager()->getStorage('entity_test');
88
    if ($storage instanceof SqlContentEntityStorage) {
89
      // Count the actual number of rows in the field table.
90
91
      $table_mapping = $storage->getTableMapping();
      $field_table_name = $table_mapping->getDedicatedDataTableName($field_storage);
92
93
94
95
96
97
98
99
      $result = db_select($field_table_name, 't')
        ->fields('t')
        ->countQuery()
        ->execute()
        ->fetchField();
      $this->assertEqual($result, 24, 'The field table has 24 rows.');
    }

100
101
    $this->assertIdentical($field_storage->hasdata(), TRUE, 'There are entities with field data.');
    $this->assertEqual($this->storage->countFieldData($field_storage), 12, 'There are 12 entities with field data.');
102
103

    // Ensure the methods work on deleted fields.
104
105
106
    $field_storage->delete();
    $this->assertIdentical($field_storage->hasdata(), TRUE, 'There are entities with deleted field data.');
    $this->assertEqual($this->storage->countFieldData($field_storage), 12, 'There are 12 entities with deleted field data.');
107
108

    field_purge_batch(6);
109
110
    $this->assertIdentical($field_storage->hasdata(), TRUE, 'There are entities with deleted field data.');
    $this->assertEqual($this->storage->countFieldData($field_storage), 6, 'There are 6 entities with deleted field data.');
111
112
113
114
115
116
117
118
119
120

    $entity_type = 'entity_test_rev';
    $this->createFieldWithStorage('_2', $entity_type);

    $entity_init = entity_create($entity_type, array(
      'type' => $entity_type,
    ));
    $cardinality = $this->fieldTestData->field_storage_2->getCardinality();

    $this->assertIdentical($this->fieldTestData->field_storage_2->hasData(), FALSE, 'There are no entities with field data.');
121
    $this->assertIdentical($this->storageRev->countFieldData($this->fieldTestData->field_storage_2), 0, 'There are 0 entities with field data.');
122
123
124
125
126
127
128
129
130
131

    // Create 1 entity with the field.
    $entity = clone($entity_init);
    $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality());
    $entity->{$this->fieldTestData->field_name_2} = $values;
    $entity->setNewRevision();
    $entity->save();
    $first_revision = $entity->getRevisionId();

    $this->assertIdentical($this->fieldTestData->field_storage_2->hasData(), TRUE, 'There are entities with field data.');
132
    $this->assertIdentical($this->storageRev->countFieldData($this->fieldTestData->field_storage_2), 1, 'There is 1 entity with field data.');
133
134
135
136
137
138
139
140
141
142

    $entity->{$this->fieldTestData->field_name_2} = array();
    $entity->setNewRevision();
    $entity->save();

    $this->assertIdentical($this->fieldTestData->field_storage_2->hasData(), TRUE, 'There are entities with field data.');

    $storage =  $this->container->get('entity.manager')->getStorage($entity_type);
    $entity = $storage->loadRevision($first_revision);
    $this->assertEqual(count($entity->{$this->fieldTestData->field_name_2}), $cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $first_revision)));
143
144
  }

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  /**
   * Verify that we can count a table that contains an entry with index 0.
   */
  public function testCountWithIndex0() {
    // Create an entry for the anonymous user, who has user ID 0.
    $user = $this->storageUser
      ->create(array(
        'uid' => 0,
        'name' => 'anonymous',
        'mail' => NULL,
        'status' => FALSE,
      ));
    $user->save();

    $storage = $user->getFieldDefinition('name')->getFieldStorageDefinition();
    $this->assertIdentical(TRUE, $this->storageUser->countFieldData($storage, TRUE));
  }

163
}