updated serial field
This commit is contained in:
191
sites/all/modules/contrib/fields/serial/tests/serial.fields.inc
Normal file
191
sites/all/modules/contrib/fields/serial/tests/serial.fields.inc
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Serial (Fields Helper).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class SerialFields.
|
||||
*/
|
||||
class SerialFields {
|
||||
|
||||
private $fields = array();
|
||||
private $instances = array();
|
||||
|
||||
/**
|
||||
* Set field definitions.
|
||||
*
|
||||
* @param array[] $fields
|
||||
* An associative array where keys - field names and values - definition.
|
||||
*/
|
||||
public function __construct(array $fields) {
|
||||
$this->fields = $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create fields.
|
||||
*
|
||||
* @return self
|
||||
* Object instance.
|
||||
*
|
||||
* @throws \FieldException
|
||||
* When cannot create a field.
|
||||
*/
|
||||
public function create() {
|
||||
foreach ($this->fields as $name => $data) {
|
||||
if (!db_table_exists("field_data_$name")) {
|
||||
field_create_field($data + array(
|
||||
'default' => '',
|
||||
'not null' => TRUE,
|
||||
'field_name' => $name,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Completely delete fields.
|
||||
*
|
||||
* This function deletes tables: "field_data_NAME" and "field_revision_NAME"
|
||||
* and entries in "field_config" and "field_config_instances".
|
||||
*
|
||||
* @return self
|
||||
* Object instance.
|
||||
*/
|
||||
public function delete() {
|
||||
foreach (array_keys($this->fields) as $name) {
|
||||
// Delete tables.
|
||||
foreach (array('data', 'revision') as $table_type) {
|
||||
$table = "field_{$table_type}_{$name}";
|
||||
|
||||
if (db_table_exists($table)) {
|
||||
db_drop_table($table);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete entries.
|
||||
foreach (array('config', 'config_instance') as $table_type) {
|
||||
db_delete("field_$table_type")
|
||||
->condition('field_name', $name)
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach existing fields into entity.
|
||||
*
|
||||
* @param string $entity_type
|
||||
* Entity machine name.
|
||||
* @param string $bundle_name
|
||||
* Entity bundle name.
|
||||
*
|
||||
* @return self
|
||||
* Object instance.
|
||||
*
|
||||
* @throws \FieldException
|
||||
* When instance cannot be created.
|
||||
*/
|
||||
public function attach($entity_type, $bundle_name) {
|
||||
$attached_fields = field_info_instances($entity_type, $bundle_name);
|
||||
|
||||
foreach ($this->fields as $field_name => $data) {
|
||||
if (empty($attached_fields[$field_name]) && field_info_field($field_name)) {
|
||||
// Provide a possibility to specify field weight, depending on
|
||||
// another one.
|
||||
//
|
||||
// @code
|
||||
// $fields = array(
|
||||
// 'field_title' => array(
|
||||
// 'type' => 'text',
|
||||
// 'label' => 'Title',
|
||||
// 'widget' => array(
|
||||
// 'weight' => 10,
|
||||
// ),
|
||||
// ),
|
||||
// 'field_description' => array(
|
||||
// 'type' => 'text',
|
||||
// 'label' => 'Description',
|
||||
// 'widget' => array(
|
||||
// // Weight of this field will be "9".
|
||||
// 'weight' => array('field_title', -1),
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// @endcode
|
||||
if (isset($data['widget']['weight']) && is_array($data['widget']['weight'])) {
|
||||
list($dependent, $calc) = $data['widget']['weight'];
|
||||
|
||||
$dependent = field_info_instance($entity_type, $dependent, $bundle_name);
|
||||
|
||||
if (!empty($dependent)) {
|
||||
$data['widget']['weight'] = $dependent['widget']['weight'] + $calc;
|
||||
}
|
||||
}
|
||||
|
||||
field_create_instance($data + array(
|
||||
'bundle' => $bundle_name,
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => $entity_type,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field instances.
|
||||
*
|
||||
* @return array[]
|
||||
* Field instances.
|
||||
*/
|
||||
public function &getInstances() {
|
||||
if (empty($this->instances)) {
|
||||
$query = db_select('field_config_instance', 'fci')
|
||||
->fields('fci', array('field_name', 'data'))
|
||||
->condition('field_name', array_keys($this->fields))
|
||||
->execute()
|
||||
->fetchAllKeyed();
|
||||
|
||||
$this->instances = array_map('unserialize', $query);
|
||||
}
|
||||
|
||||
return $this->instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Field definitions getter.
|
||||
*
|
||||
* @return array[]
|
||||
* Field definitions.
|
||||
*/
|
||||
public function getFields() {
|
||||
return $this->fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save field instances.
|
||||
*
|
||||
* @return self
|
||||
* Object instance.
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \InvalidMergeQueryException
|
||||
*/
|
||||
public function saveInstances() {
|
||||
foreach ($this->instances as $field_name => $data) {
|
||||
db_merge('field_config_instance')
|
||||
->fields(array('data' => serialize($data)))
|
||||
->condition('field_name', $field_name)
|
||||
->execute();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
128
sites/all/modules/contrib/fields/serial/tests/serial.test
Normal file
128
sites/all/modules/contrib/fields/serial/tests/serial.test
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Test Serial functionality.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class SerialTestCase.
|
||||
*/
|
||||
class SerialTestCase extends DrupalWebTestCase {
|
||||
|
||||
const CONTENT_TYPE = 'serial_test_content_type';
|
||||
|
||||
/**
|
||||
* @var \SerialFields
|
||||
*/
|
||||
private $fields;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Serial Field',
|
||||
'group' => 'Field',
|
||||
'description' => 'Testing serial field functionality.',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp('serial', 'comment');
|
||||
|
||||
$this->drupalCreateContentType(array('type' => static::CONTENT_TYPE));
|
||||
|
||||
$this->fields = new SerialFields(array(
|
||||
'serial' => array(
|
||||
'type' => SERIAL_FIELD_TYPE,
|
||||
'label' => 'Serial',
|
||||
'settings' => array(),
|
||||
),
|
||||
));
|
||||
|
||||
$this->fields
|
||||
->create()
|
||||
// Attach serial field to content type and comments form.
|
||||
->attach('node', static::CONTENT_TYPE)
|
||||
->attach('comment', 'comment_node_' . static::CONTENT_TYPE);
|
||||
|
||||
// Grant all known permissions for user.
|
||||
$this->drupalLogin($this->drupalCreateUser(array_keys(module_invoke_all('permission'))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create N nodes and attach N comments for the last.
|
||||
*
|
||||
* @param int $nodes
|
||||
* Number of nodes for creation.
|
||||
* @param int $comments
|
||||
* Number of comments for creation.
|
||||
*/
|
||||
public function testSerial($nodes = 15, $comments = 6) {
|
||||
for ($i = 0; $i < $nodes; $i++) {
|
||||
// Open form for add new node.
|
||||
$this->visit('node/add/' . str_replace('_', '-', static::CONTENT_TYPE));
|
||||
// Submit new node with filled title.
|
||||
$this->drupalPost(NULL, array('title' => "Node $i"), t('Save'));
|
||||
}
|
||||
|
||||
// Go to editing of the last created node.
|
||||
$this->visit("node/$nodes/edit");
|
||||
// Check that last created node number equal to serial ID.
|
||||
$this->assertSerialField($nodes);
|
||||
// Go to viewing of the last created node.
|
||||
$this->visit("node/$nodes");
|
||||
|
||||
// Post comments for last created node.
|
||||
for ($i = 0; $i < $comments; $i++) {
|
||||
$this->drupalPost(NULL, array(self::fieldName('comment_body') => "Comment $i"), t('Save'));
|
||||
}
|
||||
|
||||
// Go to editing of the last created comment.
|
||||
$this->visit("comment/$comments/edit");
|
||||
// Ensure the last-posted comment number equal to serial ID.
|
||||
$this->assertSerialField($comments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert number with value of the serial field on the page.
|
||||
*
|
||||
* @param int $number
|
||||
* The number for verification.
|
||||
*/
|
||||
private function assertSerialField($number) {
|
||||
$this->assertFieldByXPath($this->constructFieldXpath('name', self::fieldName('serial')), $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visit path and assert response code.
|
||||
*
|
||||
* @param string $path
|
||||
* Path to visit.
|
||||
* @param int $code
|
||||
* Expected response code.
|
||||
*/
|
||||
private function visit($path, $code = 200) {
|
||||
$this->drupalGet($path);
|
||||
$this->assertResponse($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Drupal field name into HTML.
|
||||
*
|
||||
* @param string $name
|
||||
* Drupal field name.
|
||||
* @param string $column
|
||||
* Field column.
|
||||
*
|
||||
* @return string
|
||||
* HTML input name.
|
||||
*/
|
||||
private static function fieldName($name, $column = 'value') {
|
||||
return $name . '[' . LANGUAGE_NONE . '][0][' . $column . ']';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user