Mercurial > hg > rr-repo
view sites/all/modules/bundle_inherit/bundle_inherit.test @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
line wrap: on
line source
<?php /** * @file * Tests for bundle_inherit module. */ class BundleInherit extends DrupalWebTestCase { protected $parentType, $childType, $customFields, $extraField; public static function getInfo() { return array( 'name' => 'Bundle Inherit', 'description' => 'Ensure that the Bundle Inherit module works properly', 'group' => 'Bundle Inherit' ); } public function setUp($modules = array()) { $modules += array('bundle_inherit', 'node'); parent::setUp($modules); // Add new content type (for example we'll create node type, but generaly, // kind of the entity type does not matter here) $this->parentType = $this->drupalCreateContentType(); $this->childType = $this->drupalCreateContentType(); // Create custom field do { $field_name = drupal_strtolower($this->randomName()); } while (field_info_field($field_name)); // Add 5 custom fields for ($i = 0; $i < 5; $i++) { do { $field_name = drupal_strtolower($this->randomName()); } while (field_info_field($field_name)); $this->customFields[$field_name]['field'] = field_create_field(array( 'field_name' => $field_name, 'type' => 'text' )); field_create_instance(array( 'field_name' => $field_name, 'entity_type' => 'node', 'bundle' => $this->parentType->type )); $this->customFields[$field_name]['instance'] = field_info_instance('node', $field_name, $this->parentType->type); } do { $field_name = drupal_strtolower($this->randomName()); } while (field_info_field($field_name)); $this->extraField['field'] = field_create_field(array( 'field_name' => $field_name, 'type' => 'text' )); } public function inheritPerform() { // When implementing inheritance on node entity type we should delete body // field first. if ($body_field_instance = field_info_instance('node', 'body', $this->childType->type)) { field_delete_instance($body_field_instance); } // Perform inherit operations bundle_inherit_perform('node', $this->childType->type, $this->parentType->type, TRUE); $type_inherited = db_query('SELECT 1 FROM {bundle_hierarchy} WHERE entity_type = :entity_type AND bundle = :bundle AND bundle_parent = :bundle_parent', array(':entity_type' => 'node', ':bundle' => $this->childType->type, ':bundle_parent' => $this->parentType->type))->fetchField(); $this->assertTrue($type_inherited, t('Type was inherited.')); // Check if fields instances was succesfuly inherited $n = 1; foreach ($this->customFields as $field_name => $field) { $parent_type_field_instance = $this->customFields[$field_name]['instance']; $child_type_field_instance = field_info_instance('node', $field_name, $this->childType->type); $this->assertTrue($this->compareInstances($parent_type_field_instance, $child_type_field_instance), t('Child field instance %n is equal to the parent one', array('%n' => $n++))); } } public function fieldInstanceCreate() { field_create_instance(array( 'field_name' => $this->extraField['field']['field_name'], 'entity_type' => 'node', 'bundle' => $this->parentType->type )); $this->extraField['instance'] = field_info_instance('node', $this->extraField['field']['field_name'], $this->parentType->type); $parent_type_field_instance = $this->extraField['instance']; $child_type_field_instance = field_info_instance('node', $this->extraField['field']['field_name'], $this->childType->type); $this->assertTrue($this->compareInstances($parent_type_field_instance, $child_type_field_instance), t('The field instance attached to the parent type was succesfuly attached to the child type and equal to the parent one.')); } public function fieldInstanceUpdate() { $this->extraField['instance']['label'] = $this->randomString(); $this->extraField['instance']['required'] = TRUE; field_update_instance($this->extraField['instance']); $child_type_field_instance = field_info_instance('node', $this->extraField['field']['field_name'], $this->childType->type); $this->assertTrue($this->compareInstances($this->extraField['instance'], $child_type_field_instance), t('Child type field instance is equal to the parent one after updating.')); } public function fieldInstanceDelete() { field_delete_instance($this->extraField['instance']); $child_type_field_instance = field_info_instance('node', $this->extraField['field']['field_name'], $this->childType->type); $this->assertTrue($child_type_field_instance['locked'] == FALSE, t('Child type field instance was succesfuly deleted when the parent type one was deleted.')); } /** * Run complex CRUD test based on node entity type. */ public function testCRUD() { $this->inheritPerform(); $this->fieldInstanceCreate(); $this->fieldInstanceUpdate(); $this->fieldInstanceDelete(); } /** * Helper function to compare two instances indeferent of their 'id', * 'bundle'and 'locked' fields. * @param $a * First instance. * @param $b * Second instance. * @return * TRUE if $a and $b are equal. */ private function compareInstances($a, $b) { unset($a['id'], $a['bundle'], $a['locked']); unset($b['id'], $b['bundle'], $b['locked']); return $a == $b; } }