Commit 4ffebc57 authored by webchick's avatar webchick

#368639 by puradata: Remove drupalCreateField/Instance in favour of standard API functions.

parent d30a41e7
<?php
// $Id$
// TODO : use drupalCreateField() / drupalCreateFieldInstance() all over ?
class FieldAttachTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
......@@ -1013,8 +1011,10 @@ class FieldTestCase extends DrupalWebTestCase {
// TODO: Also test deletion of the data stored in the field ?
// Create two fields (so we can test that only one is deleted).
$this->field = $this->drupalCreateField('test_field', 'test_field_name');
$this->another_field = $this->drupalCreateField('test_field', 'another_test_field_name');
$this->field = array('field_name' => 'test_field_name', 'type' => 'test_field');
field_create_field($this->field);
$this->another_field = array('field_name' => 'another_test_field_name', 'type' => 'test_field');
field_create_field($this->another_field);
// Create instances for each.
$this->instance_definition = array(
......@@ -1073,8 +1073,11 @@ class FieldInstanceTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('field_sql_storage', 'field', 'field_test');
$this->field = $this->drupalCreateField('test_field');
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'test_field',
);
field_create_field($this->field);
$this->instance_definition = array(
'field_name' => $this->field['field_name'],
'bundle' => FIELD_TEST_BUNDLE,
......
......@@ -27,13 +27,31 @@ class TextFieldTestCase extends DrupalWebTestCase {
function testTextFieldValidation() {
// Create a field with settings to validate.
$max_length = 3;
$field = $this->drupalCreateField('text', NULL, array('settings' => array('max_length' => $max_length)));
$this->instance = $this->drupalCreateFieldInstance($field['field_name'], 'text_textfield', 'text_default', FIELD_TEST_BUNDLE);
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'text',
'settings' => array(
'max_length' => $max_length,
)
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'bundle' => FIELD_TEST_BUNDLE,
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'full' => array(
'type' => 'text_default',
),
),
);
field_create_instance($this->instance);
// Test valid and invalid values with field_attach_validate().
$entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
for ($i = 0; $i <= $max_length + 2; $i++) {
$entity->{$field['field_name']}[0]['value'] = str_repeat('x', $i);
$entity->{$this->field['field_name']}[0]['value'] = str_repeat('x', $i);
try {
field_attach_validate('test_entity', $entity);
$this->assertTrue($i <= $max_length, "Length $i does not cause validation error when max_length is $max_length");
......
......@@ -2086,58 +2086,5 @@ protected function assertResponse($code, $message = '') {
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
}
/**
* TODO write documentation.
* @param $type
* @param $field_name
* @param $settings
* @return unknown_type
*/
protected function drupalCreateField($type, $field_name = NULL, $settings = array()) {
if (!isset($field_name)) {
$field_name = strtolower($this->randomName());
}
$field_definition = array(
'field_name' => $field_name,
'type' => $type,
);
$field_definition += $settings;
field_create_field($field_definition);
$field = field_read_field($field_name);
$this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type)));
return $field;
}
/**
* TODO write documentation.
* @param $field_name
* @param $widget_type
* @param $display_type
* @param $bundle
* @return unknown_type
*/
protected function drupalCreateFieldInstance($field_name, $widget_type, $formatter_type, $bundle) {
$instance_definition = array(
'field_name' => $field_name,
'bundle' => $bundle,
'widget' => array(
'type' => $widget_type,
),
'display' => array(
'full' => array(
'type' => $formatter_type,
),
),
);
field_create_instance($instance_definition);
$instance = field_read_instance($field_name, $bundle);
$this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle)));
return $instance;
}
}
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