Commit 38a36d4e authored by webchick's avatar webchick

Issue #1253820 follow-up by plach: Missing a few hook implementations + tests.

parent 252f0c8e
......@@ -244,6 +244,10 @@ public function create(array $values) {
$entity->{$this->uuidKey} = $uuid->generate();
}
// Modules might need to add or change the data initially held by the new
// entity object, for instance to fill-in default values.
$this->invokeHook('create', $entity);
return $entity;
}
......
......@@ -104,6 +104,11 @@ public function create(array $values) {
$uuid = new Uuid();
$entity->{$this->uuidKey}->value = $uuid->generate();
}
// Modules might need to add or change the data initially held by the new
// entity object, for instance to fill-in default values.
$this->invokeHook('create', $entity);
return $entity;
}
......
......@@ -55,24 +55,11 @@ protected function attachLoad(&$records, $load_revision = FALSE) {
* Overrides Drupal\Core\Entity\DatabaseStorageControllerNG::create().
*/
public function create(array $values) {
// We have to determine the bundle first.
if (empty($values['node_type']) && !empty($values['nid'])) {
$node = node_load($values['nid']);
$values['node_type'] = 'comment_node_' . $node->type;
}
$comment = new $this->entityClass(array(), $this->entityType, $values['node_type']);
// Set all other given values.
foreach ($values as $name => $value) {
$comment->$name = $value;
}
// Assign a new UUID if there is none yet.
if ($this->uuidKey && !isset($comment->{$this->uuidKey})) {
$uuid = new Uuid();
$comment->{$this->uuidKey}->value = $uuid->generate();
}
return $comment;
return parent::create($values);
}
/**
......
......@@ -175,6 +175,11 @@ function testCRUD() {
$this->assertIdentical($config_test->id(), $new_id);
$this->assertIdentical($config_test->getOriginalID(), $new_id);
}
// Test config entity prepopulation.
state()->set('config_test.prepopulate', TRUE);
$config_test = entity_create('config_test', array('foo' => 'bar'));
$this->assertEqual($config_test->get('foo'), 'baz', 'Initial value correctly populated');
}
/**
......
......@@ -137,3 +137,12 @@ function config_test_cache_flush() {
return array();
}
/**
* Implements hook_ENTITY_TYPE_create().
*/
function config_test_config_test_create(ConfigTest $config_test) {
if (state()->get('config_test.prepopulate')) {
$config_test->set('foo', 'baz');
}
}
......@@ -36,7 +36,7 @@ class EntityCrudHookTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Entity CRUD hooks',
'description' => 'Tests the invocation of hooks when inserting, loading, updating or deleting an entity.',
'description' => 'Tests the invocation of hooks when creating, inserting, loading, updating or deleting an entity.',
'group' => 'Entity API',
);
}
......@@ -74,6 +74,12 @@ public function testBlockHooks() {
'id' => 'stark.test_html_id',
'plugin' => 'test_html_id',
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_block_create called',
'entity_crud_hook_test_entity_create called for type block',
));
$_SESSION['entity_crud_hook_test'] = array();
$entity->save();
......@@ -132,6 +138,7 @@ public function testCommentHooks() {
));
$node->save();
$nid = $node->nid;
$_SESSION['entity_crud_hook_test'] = array();
$comment = entity_create('comment', array(
'node_type' => 'node_type_' . $node->bundle(),
......@@ -146,6 +153,11 @@ public function testCommentHooks() {
'langcode' => LANGUAGE_NOT_SPECIFIED,
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_comment_create called',
'entity_crud_hook_test_entity_create called for type comment',
));
$_SESSION['entity_crud_hook_test'] = array();
comment_save($comment);
......@@ -202,6 +214,12 @@ public function testFileHooks() {
'status' => 1,
'timestamp' => REQUEST_TIME,
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_file_create called',
'entity_crud_hook_test_entity_create called for type file',
));
$_SESSION['entity_crud_hook_test'] = array();
$file->save();
......@@ -258,6 +276,12 @@ public function testNodeHooks() {
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_node_create called',
'entity_crud_hook_test_entity_create called for type node',
));
$_SESSION['entity_crud_hook_test'] = array();
$node->save();
......@@ -310,6 +334,7 @@ public function testTaxonomyTermHooks() {
'module' => 'entity_crud_hook_test',
));
taxonomy_vocabulary_save($vocabulary);
$_SESSION['entity_crud_hook_test'] = array();
$term = entity_create('taxonomy_term', array(
'vid' => $vocabulary->id(),
......@@ -318,6 +343,12 @@ public function testTaxonomyTermHooks() {
'description' => NULL,
'format' => 1,
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_taxonomy_term_create called',
'entity_crud_hook_test_entity_create called for type taxonomy_term',
));
$_SESSION['entity_crud_hook_test'] = array();
taxonomy_term_save($term);
......@@ -369,6 +400,12 @@ public function testTaxonomyVocabularyHooks() {
'description' => NULL,
'module' => 'entity_crud_hook_test',
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_taxonomy_vocabulary_create called',
'entity_crud_hook_test_entity_create called for type taxonomy_vocabulary',
));
$_SESSION['entity_crud_hook_test'] = array();
taxonomy_vocabulary_save($vocabulary);
......@@ -420,6 +457,12 @@ public function testUserHooks() {
'status' => 1,
'language' => 'en',
));
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_user_create called',
'entity_crud_hook_test_entity_create called for type user',
));
$_SESSION['entity_crud_hook_test'] = array();
$account->save();
......
......@@ -7,6 +7,62 @@
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_create().
*/
function entity_crud_hook_test_entity_create(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
* Implements hook_block_create().
*/
function entity_crud_hook_test_block_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_comment_create().
*/
function entity_crud_hook_test_comment_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_file_create().
*/
function entity_crud_hook_test_file_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_node_create().
*/
function entity_crud_hook_test_node_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_taxonomy_term_create().
*/
function entity_crud_hook_test_taxonomy_term_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_taxonomy_vocabulary_create().
*/
function entity_crud_hook_test_taxonomy_vocabulary_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_user_create().
*/
function entity_crud_hook_test_user_create() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_entity_presave().
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment