first import
This commit is contained in:
14
sites/all/modules/entity/tests/entity_feature.info
Normal file
14
sites/all/modules/entity/tests/entity_feature.info
Normal file
@@ -0,0 +1,14 @@
|
||||
name = Entity feature module
|
||||
description = Provides some entities in code.
|
||||
version = VERSION
|
||||
core = 7.x
|
||||
files[] = entity_feature.module
|
||||
dependencies[] = entity_test
|
||||
hidden = TRUE
|
||||
|
||||
; Information added by drupal.org packaging script on 2012-12-25
|
||||
version = "7.x-1.0"
|
||||
core = "7.x"
|
||||
project = "entity"
|
||||
datestamp = "1356471145"
|
||||
|
32
sites/all/modules/entity/tests/entity_feature.module
Normal file
32
sites/all/modules/entity/tests/entity_feature.module
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test module providing some entities in code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_default_entity_test_type().
|
||||
*/
|
||||
function entity_feature_default_entity_test_type() {
|
||||
$types['main'] = entity_create('entity_test_type', array(
|
||||
'name' => 'main',
|
||||
'label' => t('Main test type'),
|
||||
'weight' => 0,
|
||||
'locked' => TRUE,
|
||||
));
|
||||
|
||||
// Types used during CRUD testing.
|
||||
$types['test'] = entity_create('entity_test_type', array(
|
||||
'name' => 'test',
|
||||
'label' => 'label',
|
||||
'weight' => 0,
|
||||
));
|
||||
$types['test2'] = entity_create('entity_test_type', array(
|
||||
'name' => 'test2',
|
||||
'label' => 'label2',
|
||||
'weight' => 2,
|
||||
));
|
||||
|
||||
return $types;
|
||||
}
|
15
sites/all/modules/entity/tests/entity_test.info
Normal file
15
sites/all/modules/entity/tests/entity_test.info
Normal file
@@ -0,0 +1,15 @@
|
||||
name = Entity CRUD test module
|
||||
description = Provides entity types based upon the CRUD API.
|
||||
version = VERSION
|
||||
core = 7.x
|
||||
files[] = entity_test.module
|
||||
files[] = entity_test.install
|
||||
dependencies[] = entity
|
||||
hidden = TRUE
|
||||
|
||||
; Information added by drupal.org packaging script on 2012-12-25
|
||||
version = "7.x-1.0"
|
||||
core = "7.x"
|
||||
project = "entity"
|
||||
datestamp = "1356471145"
|
||||
|
164
sites/all/modules/entity/tests/entity_test.install
Normal file
164
sites/all/modules/entity/tests/entity_test.install
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update and uninstall functions for the entity_test module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function entity_test_uninstall() {
|
||||
// Bypass entity_load() as we cannot use it here.
|
||||
$types = db_select('entity_test_type', 'et')
|
||||
->fields('et')
|
||||
->execute()
|
||||
->fetchAllAssoc('name');
|
||||
|
||||
foreach ($types as $name => $type) {
|
||||
field_attach_delete_bundle('entity_test', $name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_schema().
|
||||
*/
|
||||
function entity_test_schema() {
|
||||
$schema['entity_test'] = array(
|
||||
'description' => 'Stores entity_test items.',
|
||||
'fields' => array(
|
||||
'pid' => array(
|
||||
'type' => 'serial',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary Key: Unique entity_test item ID.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The name of the entity_test.',
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
),
|
||||
'uid' => array(
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => FALSE,
|
||||
'default' => NULL,
|
||||
'description' => "The {users}.uid of the associated user.",
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'uid' => array('uid'),
|
||||
),
|
||||
'foreign keys' => array(
|
||||
'uid' => array('users' => 'uid'),
|
||||
'name' => array('entity_test_types' => 'name'),
|
||||
),
|
||||
'primary key' => array('pid'),
|
||||
);
|
||||
|
||||
$schema['entity_test_type'] = array(
|
||||
'description' => 'Stores information about all defined entity_test types.',
|
||||
'fields' => array(
|
||||
'id' => array(
|
||||
'type' => 'serial',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary Key: Unique entity_test type ID.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The machine-readable name of this entity_test type.',
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
),
|
||||
'label' => array(
|
||||
'description' => 'The human-readable name of this entity_test type.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
),
|
||||
'weight' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'size' => 'tiny',
|
||||
'description' => 'The weight of this entity_test type in relation to others.',
|
||||
),
|
||||
'locked' => array(
|
||||
'description' => 'A boolean indicating whether the administrator may delete this type.',
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'size' => 'tiny',
|
||||
),
|
||||
'data' => array(
|
||||
'type' => 'text',
|
||||
'not null' => FALSE,
|
||||
'size' => 'big',
|
||||
'serialize' => TRUE,
|
||||
'description' => 'A serialized array of additional data related to this entity_test type.',
|
||||
'merge' => TRUE,
|
||||
),
|
||||
'status' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
// Set the default to ENTITY_CUSTOM without using the constant as it is
|
||||
// not safe to use it at this point.
|
||||
'default' => 0x01,
|
||||
'size' => 'tiny',
|
||||
'description' => 'The exportable status of the entity.',
|
||||
),
|
||||
'module' => array(
|
||||
'description' => 'The name of the providing module if the entity has been defined in code.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => FALSE,
|
||||
),
|
||||
),
|
||||
'primary key' => array('id'),
|
||||
'unique keys' => array(
|
||||
'name' => array('name'),
|
||||
),
|
||||
);
|
||||
|
||||
// Add schema for the revision-test-entity.
|
||||
$schema['entity_test2'] = $schema['entity_test'];
|
||||
$schema['entity_test2']['fields']['revision_id'] = array(
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => FALSE,
|
||||
'default' => NULL,
|
||||
'description' => 'The ID of the entity\'s default revision.',
|
||||
);
|
||||
$schema['entity_test2']['fields']['title'] = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
);
|
||||
|
||||
$schema['entity_test2_revision'] = $schema['entity_test'];
|
||||
$schema['entity_test2_revision']['fields']['revision_id'] = array(
|
||||
'type' => 'serial',
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary Key: Unique revision ID.',
|
||||
);
|
||||
$schema['entity_test2_revision']['fields']['pid'] = array(
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => FALSE,
|
||||
'default' => NULL,
|
||||
'description' => 'The ID of the attached entity.',
|
||||
);
|
||||
$schema['entity_test2_revision']['fields']['title'] = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
);
|
||||
$schema['entity_test2_revision']['primary key'] = array('revision_id');
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
280
sites/all/modules/entity/tests/entity_test.module
Normal file
280
sites/all/modules/entity/tests/entity_test.module
Normal file
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test moduel for the entity API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_entity_info().
|
||||
*/
|
||||
function entity_test_entity_info() {
|
||||
$return = array(
|
||||
'entity_test' => array(
|
||||
'label' => t('Test Entity'),
|
||||
'plural label' => t('Test Entities'),
|
||||
'description' => t('An entity type used by the entity API tests.'),
|
||||
'entity class' => 'EntityClass',
|
||||
'controller class' => 'EntityAPIController',
|
||||
'base table' => 'entity_test',
|
||||
'fieldable' => TRUE,
|
||||
'entity keys' => array(
|
||||
'id' => 'pid',
|
||||
'bundle' => 'name',
|
||||
),
|
||||
// Make use the class' label() and uri() implementation by default.
|
||||
'label callback' => 'entity_class_label',
|
||||
'uri callback' => 'entity_class_uri',
|
||||
'bundles' => array(),
|
||||
'bundle keys' => array(
|
||||
'bundle' => 'name',
|
||||
),
|
||||
'module' => 'entity_test',
|
||||
),
|
||||
'entity_test_type' => array(
|
||||
'label' => t('Test entity type'),
|
||||
'entity class' => 'Entity',
|
||||
'controller class' => 'EntityAPIControllerExportable',
|
||||
'base table' => 'entity_test_type',
|
||||
'fieldable' => FALSE,
|
||||
'bundle of' => 'entity_test',
|
||||
'exportable' => TRUE,
|
||||
'entity keys' => array(
|
||||
'id' => 'id',
|
||||
'name' => 'name',
|
||||
),
|
||||
'module' => 'entity_test',
|
||||
),
|
||||
|
||||
'entity_test2' => array(
|
||||
'label' => t('Test Entity (revision support)'),
|
||||
'entity class' => 'EntityClassRevision',
|
||||
'controller class' => 'EntityAPIController',
|
||||
'base table' => 'entity_test2',
|
||||
'revision table' => 'entity_test2_revision',
|
||||
'fieldable' => TRUE,
|
||||
'entity keys' => array(
|
||||
'id' => 'pid',
|
||||
'revision' => 'revision_id',
|
||||
),
|
||||
// Make use of the class label() and uri() implementation by default.
|
||||
'label callback' => 'entity_class_label',
|
||||
'uri callback' => 'entity_class_uri',
|
||||
'bundles' => array(),
|
||||
'bundle keys' => array(
|
||||
'bundle' => 'name',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Add bundle info but bypass entity_load() as we cannot use it here.
|
||||
$types = db_select('entity_test_type', 'et')
|
||||
->fields('et')
|
||||
->execute()
|
||||
->fetchAllAssoc('name');
|
||||
|
||||
foreach ($types as $name => $type) {
|
||||
$return['entity_test']['bundles'][$name] = array(
|
||||
'label' => $type->label,
|
||||
);
|
||||
}
|
||||
|
||||
// Support entity cache module.
|
||||
if (module_exists('entitycache')) {
|
||||
$return['entity_test']['field cache'] = FALSE;
|
||||
$return['entity_test']['entity cache'] = TRUE;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of all test entity types, keyed by the name.
|
||||
*
|
||||
* @param $name
|
||||
* If set, the type with the given name is returned.
|
||||
*/
|
||||
function entity_test_get_types($name = NULL) {
|
||||
$types = entity_load_multiple_by_name('entity_test_type', isset($name) ? array($name) : FALSE);
|
||||
return isset($name) ? reset($types) : $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load multiple test entities based on certain conditions.
|
||||
*
|
||||
* @param $pids
|
||||
* An array of entity IDs.
|
||||
* @param $conditions
|
||||
* An array of conditions to match against the {entity} table.
|
||||
* @param $reset
|
||||
* A boolean indicating that the internal cache should be reset.
|
||||
* @return
|
||||
* An array of test entity objects, indexed by pid.
|
||||
*/
|
||||
function entity_test_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
|
||||
return entity_load('entity_test', $pids, $conditions, $reset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete multiple test entities.
|
||||
*
|
||||
* @param $pids
|
||||
* An array of test entity IDs.
|
||||
*/
|
||||
function entity_test_delete_multiple(array $pids) {
|
||||
entity_get_controller('entity_test')->delete($pids);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Main class for test entities.
|
||||
*/
|
||||
class EntityClass extends Entity {
|
||||
|
||||
public function __construct(array $values = array(), $entityType = NULL) {
|
||||
parent::__construct($values, 'entity_test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Override buildContent() to add the username to the output.
|
||||
*/
|
||||
public function buildContent($view_mode = 'full', $langcode = NULL) {
|
||||
$content['user'] = array(
|
||||
'#markup' => "User: ". format_username(user_load($this->uid)),
|
||||
);
|
||||
return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the default label, which is picked up by label() by default.
|
||||
*/
|
||||
protected function defaultLabel() {
|
||||
$type = entity_test_get_types($this->name);
|
||||
return $type->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the default uri, which is picked up by uri() by default.
|
||||
*/
|
||||
protected function defaultURI() {
|
||||
return array('path' => 'custom/' . $this->identifier());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main class for test entities (with revision support).
|
||||
*/
|
||||
class EntityClassRevision extends EntityClass {
|
||||
|
||||
public function __construct(array $values = array(), $entityType = NULL) {
|
||||
Entity::__construct($values, 'entity_test2');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Some hook implementations used by the tests.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_entity_insert().
|
||||
*/
|
||||
function entity_test_entity_insert($entity, $entity_type) {
|
||||
if ($entity_type == 'entity_test_type') {
|
||||
$_SESSION['entity_hook_test']['entity_insert'][] = entity_id($entity_type, $entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_update().
|
||||
*/
|
||||
function entity_test_entity_update($entity, $entity_type) {
|
||||
$_SESSION['entity_hook_test']['entity_update'][] = entity_id($entity_type, $entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_delete().
|
||||
*/
|
||||
function entity_test_entity_delete($entity, $entity_type) {
|
||||
if ($entity_type == 'entity_test_type') {
|
||||
$_SESSION['entity_hook_test']['entity_delete'][] = entity_id($entity_type, $entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_test_type_insert().
|
||||
*/
|
||||
function entity_test_entity_test_type_insert($entity) {
|
||||
$_SESSION['entity_hook_test']['entity_test_type_insert'][] = $entity->identifier();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_test_type_update().
|
||||
*/
|
||||
function entity_test_entity_test_type_update($entity) {
|
||||
$_SESSION['entity_hook_test']['entity_test_type_update'][] = $entity->identifier();
|
||||
|
||||
// Determine changes on update.
|
||||
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
|
||||
if ($entity->original->label != $entity->label) {
|
||||
$entity->label .= '_update';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_test_type_delete().
|
||||
*/
|
||||
function entity_test_entity_test_type_delete($entity) {
|
||||
$_SESSION['entity_hook_test']['entity_test_type_delete'][] = $entity->identifier();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_test_type_presave().
|
||||
*/
|
||||
function entity_test_entity_test_type_presave($entity) {
|
||||
// Determine changes.
|
||||
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
|
||||
if ($entity->original->label != $entity->label) {
|
||||
$entity->label .= '_presave';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_property_info_alter() for testing an property of type
|
||||
* 'entity'.
|
||||
*/
|
||||
function entity_test_entity_property_info_alter(&$info) {
|
||||
$info['node']['properties']['reference'] = array(
|
||||
'label' => t('Test reference'),
|
||||
'description' => t('A generic entity reference.'),
|
||||
'getter callback' => 'entity_test_entity_getter',
|
||||
'setter callback' => 'entity_test_entity_setter',
|
||||
'type' => 'entity',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter callback for the 'reference' property.
|
||||
*/
|
||||
function entity_test_entity_getter($node) {
|
||||
if (empty($node->entity)) {
|
||||
$node->entity = array('type' => 'user', 'id' => $node->uid);
|
||||
}
|
||||
// We have to return the entity wrapped.
|
||||
return entity_metadata_wrapper($node->entity['type'], $node->entity['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter callback for the 'reference' property.
|
||||
*/
|
||||
function entity_test_entity_setter($node, $property_name, $wrapper) {
|
||||
// The entity has to be passed wrapped.
|
||||
$node->entity = array('type' => $wrapper->type(), 'id' => $wrapper->getIdentifier());
|
||||
}
|
13
sites/all/modules/entity/tests/entity_test_i18n.info
Normal file
13
sites/all/modules/entity/tests/entity_test_i18n.info
Normal file
@@ -0,0 +1,13 @@
|
||||
name = Entity-test type translation
|
||||
description = Allows translating entity-test types.
|
||||
dependencies[] = entity_test
|
||||
dependencies[] = i18n_string
|
||||
package = Multilingual - Internationalization
|
||||
core = 7.x
|
||||
hidden = TRUE
|
||||
; Information added by drupal.org packaging script on 2012-12-25
|
||||
version = "7.x-1.0"
|
||||
core = "7.x"
|
||||
project = "entity"
|
||||
datestamp = "1356471145"
|
||||
|
53
sites/all/modules/entity/tests/entity_test_i18n.module
Normal file
53
sites/all/modules/entity/tests/entity_test_i18n.module
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Entity-test i18n integration module via entity API i18n support.
|
||||
*
|
||||
* @see EntityDefaultI18nController
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_entity_info_alter().
|
||||
*/
|
||||
function entity_test_i18n_entity_info_alter(&$info) {
|
||||
// Enable i18n support via the entity API.
|
||||
$info['entity_test_type']['i18n controller class'] = 'EntityDefaultI18nStringController';
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_property_info_alter().
|
||||
*/
|
||||
function entity_test_i18n_entity_property_info_alter(&$info) {
|
||||
// Mark some properties as translatable, but also denote that translation
|
||||
// works with i18n_string.
|
||||
foreach (array('label') as $name) {
|
||||
$info['entity_test_type']['properties'][$name]['translatable'] = TRUE;
|
||||
$info['entity_test_type']['properties'][$name]['i18n string'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_{entity_test_type}_insert().
|
||||
*/
|
||||
function entity_test_i18n_entity_test_type_insert($test_type) {
|
||||
i18n_string_object_update('entity_test_type', $test_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_{entity_test_type}_update().
|
||||
*/
|
||||
function entity_test_i18n_entity_test_type_update($test_type) {
|
||||
// Account for name changes.
|
||||
if ($test_type->original->name != $test_type->name) {
|
||||
i18n_string_update_context("entity_test:entity_test_type:{$test_type->original->name}:*", "entity_test:entity_test_type:{$test_type->name}:*");
|
||||
}
|
||||
i18n_string_object_update('entity_test_type', $test_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_{entity_test_type}_delete().
|
||||
*/
|
||||
function entity_test_i18n_entity_test_type_delete($test_type) {
|
||||
i18n_string_object_remove('entity_test_type', $test_type);
|
||||
}
|
Reference in New Issue
Block a user