View source
<?php
class ServicesEntityBasic extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Basic',
'description' => 'Basic tests for Services Entity.',
'group' => 'Services Entity',
);
}
function setUp() {
parent::setUp('services', 'entity', 'services_entity', 'locale');
module_load_include('inc', 'services_entity', 'services_entity.resources');
$this->adminUser = user_load(1);
require_once DRUPAL_ROOT . '/includes/locale.inc';
locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE);
}
function testPropertyFetching() {
$node = (object) array(
'title' => 'Test of title',
'type' => 'article',
'uid' => $this->adminUser->uid,
'status' => 1,
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test of body',
'format' => 'filtered_html',
),
),
),
);
node_save($node);
$expected['nid'] = array(
'cardinality' => 1,
'type' => 'integer',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => $node->nid,
),
),
);
$expected['title'] = array(
'cardinality' => 1,
'type' => 'text',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test of title',
),
),
);
$expected['type'] = array(
'cardinality' => 1,
'type' => 'token',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => 'article',
),
),
);
$expected['status'] = array(
'cardinality' => 1,
'type' => 'integer',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => 1,
),
),
);
$expected['author:name'] = array(
'cardinality' => 1,
'type' => 'user',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'name' => array(
'cardinality' => 1,
'type' => 'text',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => $this->adminUser->name,
),
),
),
),
),
);
$expected['author:uid'] = array(
'cardinality' => 1,
'type' => 'user',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'uid' => array(
'cardinality' => 1,
'type' => 'integer',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => $this->adminUser->uid,
),
),
),
),
),
);
$expected['author:created'] = array(
'cardinality' => 1,
'type' => 'user',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'created' => array(
'cardinality' => 1,
'type' => 'date',
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => $this->adminUser->created,
),
),
),
),
),
);
foreach ($expected as $property => $expected) {
$fields = _services_entity_transform_fields(array(
$property => $property,
));
$entity = clone $node;
$wrapper = entity_metadata_wrapper('node', $entity);
$result = services_entity_prepare_structure($wrapper, $fields);
$path = explode(':', $property);
$base_element = reset($path);
$this
->verbose("<pre>Expected: " . print_r($expected, TRUE) . "\n\nReturned: " . print_r($result[$base_element], TRUE) . "</pre>");
$this
->assertEqual($expected, $result[$base_element], t('Property @property returned expected result', array(
'@property' => $property,
)));
entity_get_controller('node')
->resetCache();
entity_get_controller('user')
->resetCache();
}
}
function testPropertyFetchingFromFields() {
$cases['field_text_single'] = array(
'field' => array(
'type' => 'text',
'cardinality' => 1,
'translatable' => 0,
),
);
$cases['field_text_single']['expected'][] = array(
'content' => array(),
'result' => NULL,
);
$cases['field_text_single']['expected'][] = array(
'content' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content',
),
),
),
'result' => array(
'type' => 'text',
'cardinality' => 1,
'translatable' => 0,
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content',
),
),
),
);
$cases['field_text_single_translatable'] = array(
'field' => array(
'type' => 'text',
'cardinality' => 1,
'translatable' => 1,
),
);
$cases['field_text_single_translatable']['expected'][] = array(
'content' => array(),
'result' => NULL,
);
$cases['field_text_single_translatable']['expected'][] = array(
'content' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
),
),
'result' => array(
'type' => 'text',
'cardinality' => 1,
'translatable' => 1,
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
),
),
);
$cases['field_text_single_translatable']['expected'][] = array(
'content' => array(
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
),
),
'result' => array(
'type' => 'text',
'cardinality' => 1,
'translatable' => 1,
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
),
),
);
$cases['field_text_double_translatable'] = array(
'field' => array(
'type' => 'text',
'cardinality' => 2,
'translatable' => 1,
),
);
$cases['field_text_double_translatable']['expected'][] = array(
'content' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
1 => array(
'value' => 'Test content bis (fr)',
),
),
),
'result' => array(
'type' => 'text',
'cardinality' => 2,
'translatable' => 1,
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
1 => array(
'value' => 'Test content bis (fr)',
),
),
),
);
$cases['field_text_infinite_translatable'] = array(
'field' => array(
'type' => 'text',
'cardinality' => -1,
'translatable' => 1,
),
);
$cases['field_text_infinite_translatable']['expected'][] = array(
'content' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
1 => array(
'value' => 'Test content bis (und)',
),
2 => array(
'value' => 'Test content ter (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
1 => array(
'value' => 'Test content bis (fr)',
),
),
),
'result' => array(
'type' => 'text',
'cardinality' => -1,
'translatable' => 1,
LANGUAGE_NONE => array(
0 => array(
'value' => 'Test content (und)',
),
1 => array(
'value' => 'Test content bis (und)',
),
2 => array(
'value' => 'Test content ter (und)',
),
),
'fr' => array(
0 => array(
'value' => 'Test content (fr)',
),
1 => array(
'value' => 'Test content bis (fr)',
),
),
),
);
$this
->assertFieldPropertyFetching($cases);
}
protected function assertFieldPropertyFetching(array $cases) {
foreach ($cases as $case_name => $case) {
$field_info = $case['field'];
$field_info['field_name'] = $case_name;
field_create_field($field_info);
$instance_info = array(
'entity_type' => 'node',
'bundle' => 'article',
'field_name' => $field_info['field_name'],
);
field_create_instance($instance_info);
foreach ($case['expected'] as $expected) {
$node = (object) array(
'title' => 'Test node',
'type' => 'article',
'uid' => $this->adminUser->uid,
'status' => 1,
$case_name => $expected['content'],
);
node_save($node);
$property = $case_name;
$fields = _services_entity_transform_fields(array(
$case_name => $case_name,
));
$wrapper = entity_metadata_wrapper('node', $node);
$result = services_entity_prepare_structure($wrapper, $fields);
$path = explode(':', $property);
$base_element = reset($path);
$this
->verbose("<pre>Expected: " . print_r($expected['result'], TRUE) . "\n\nReturned: " . print_r($result[$base_element], TRUE) . "</pre>");
$this
->assertEqual($expected['result'], $result[$base_element], t('Case @case, property @property returned expected result', array(
'@case' => $case_name,
'@property' => $property,
)));
}
}
}
}