/** * Test entity_bundle_create() and entity_bundle_rename(). */ function testEntityCreateRenameBundle() { $entity_type = 'entity_test_rev'; $this->createFieldWithInstance('', $entity_type); $cardinality = $this->field_storage->getCardinality(); // Create a new bundle. $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName()); entity_test_create_bundle($new_bundle, NULL, $entity_type); // Add an instance to that bundle. $this->instance_definition['bundle'] = $new_bundle; entity_create('field_instance_config', $this->instance_definition)->save(); // Save an entity with data in the field. $entity = entity_create($entity_type, array('type' => $this->instance->bundle)); $values = $this->_generateTestFieldValues($cardinality); $entity->{$this->field_name} = $values; // Verify the field data is present on load. $entity = $this->entitySaveReload($entity); $this->assertEqual(count($entity->{$this->field_name}), $cardinality, "Data is retrieved for the new bundle"); // Rename the bundle. $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName()); entity_test_rename_bundle($this->instance_definition['bundle'], $new_bundle, $entity_type); // Check that the instance definition has been updated. $this->instance = FieldInstanceConfig::loadByName($entity_type, $new_bundle, $this->field_name); $this->assertIdentical($this->instance->bundle, $new_bundle, "Bundle name has been updated in the instance."); // Verify the field data is present on load. $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId()); $controller->resetCache(); $entity = $controller->load($entity->id()); $this->assertEqual(count($entity->{$this->field_name}), $cardinality, "Bundle name has been updated in the field storage"); }
/** * Tests reacting to a bundle being renamed. */ function testFieldSqlStorageBundleRename() { $entity_type = $bundle = 'entity_test_rev'; // Create an entity. $value = mt_rand(1, 127); $entity = entity_create($entity_type, array('type' => $bundle, $this->fieldStorage->name => $value)); $entity->save(); // Rename the bundle. $bundle_new = $bundle . '_renamed'; entity_test_rename_bundle($bundle, $bundle_new, $entity_type); // Check that the 'bundle' column has been updated in storage. $row = db_select($this->table, 't')->fields('t', array('bundle', $this->fieldStorage->name . '_value'))->condition('entity_id', $entity->id())->execute()->fetch(); $this->assertEqual($row->bundle, $bundle_new); $this->assertEqual($row->{$this->fieldStorage->name . '_value'}, $value); }