comparison sites/all/modules/references/node_reference/node_reference.test @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2
3 /**
4 * @file
5 * Initial node_reference tests
6 */
7
8 /**
9 * Unit tests for referenceability of node types in entity forms.
10 */
11 class NodeReferenceFormTest extends FieldTestCase {
12 public static function getInfo() {
13 return array(
14 'name' => 'Node reference',
15 'description' => 'Make sure nodes are referenceable in entity forms.',
16 'group' => 'References',
17 );
18 }
19
20 function setUp() {
21 parent::setUp(array('node_reference', 'field_test'));
22
23 $this->langcode = LANGUAGE_NONE;
24 $this->field_name = 'test_node_reference';
25 $this->field = array(
26 'field_name' => $this->field_name,
27 'type' => 'node_reference',
28 'cardinality' => 1,
29 'settings' => array(
30 'referenceable_types' => array_keys(node_type_get_names()),
31 ),
32 );
33 $this->field = field_create_field($this->field);
34 $this->instance = array(
35 'field_name' => $this->field_name,
36 'entity_type' => 'test_entity',
37 'bundle' => 'test_bundle',
38 'widget' => array(
39 'type' => 'options_buttons',
40 ),
41 );
42
43 $this->instance = field_create_instance($this->instance);
44
45 $this->nodes = array();
46 foreach (node_type_get_names() as $type_name => $type_title) {
47 $this->nodes[$type_name] = $this->drupalCreateNode(array(
48 'type' => $type_name,
49 'title' => $this->randomName(8),
50 ));
51 $this->pass(t('Created %type node %nid: %title', array(
52 '%type' => $type_name,
53 '%nid' => $this->nodes[$type_name]->nid,
54 '%title' => $this->nodes[$type_name]->title,
55 )), 'destination creation');
56 }
57 }
58
59 function runReferenceableNodeTest($allowed, $group) {
60 field_update_field(array(
61 'field_name' => $this->field_name,
62 'settings' => array('referenceable_types' => array_keys($allowed)),
63 ));
64 $entity = field_test_create_stub_entity();
65 $form = drupal_get_form('field_test_entity_form', $entity);
66 $options = $form[$this->field_name][$this->langcode]['#options'];
67 $this->assertTrue(isset($options['_none']), t('Empty choice offered for reference'), $group);
68 unset($options['_none']);
69 foreach ($this->nodes as $node) {
70 if (isset($allowed[$node->type])) {
71 $this->assertTrue(isset($options[$node->nid]),
72 t('Node of type @type is referenceable', array('@type' => $node->type)),
73 $group);
74 }
75 else {
76 $this->assertFalse(isset($options[$node->nid]),
77 t('Node of type @type is not referenceable', array('@type' => $node->type)),
78 $group);
79 }
80 unset($options[$node->nid]);
81 }
82 $this->assertTrue(empty($options), t('No extra choice is referenceable'), $group);
83 }
84
85 /**
86 * Test unlimited referencing
87 */
88 function testReferenceableNodeTypesAll() {
89 $allowed = node_type_get_names();
90 $this->runReferenceableNodeTest($allowed, t('Unimited referencing'));
91 }
92
93 /**
94 * Test referencing a limited list of node types
95 */
96 function testReferenceableNodeTypesOne() {
97 $allowed = array_slice(node_type_get_names(), 0, 1, TRUE);
98 $this->runReferenceableNodeTest($allowed, t('Limited referencing'));
99 }
100
101
102 /**
103 * Test autocomplete widget.
104 */
105 function testLongNodeReferenceWidget() {
106 // Create regular test user.
107 $web_user = $this->drupalCreateUser(array('create article content', 'access content'));
108 $this->drupalLogin($web_user);
109
110 // Create test field instance on article node type.
111 $instance = array(
112 'field_name' => $this->field_name,
113 'entity_type' => 'node',
114 'bundle' => 'article',
115 'widget' => array(
116 'type' => 'node_reference_autocomplete',
117 ),
118 );
119 $instance = field_create_instance($instance);
120
121 // Create a node with a short title and a node with a title longer than
122 // 128 characters.
123 $node_short_title = $this->drupalCreateNode(array(
124 'type' => 'page',
125 'title' => $this->randomName(8),
126 ));
127 $node_long_title = $this->drupalCreateNode(array(
128 'type' => 'page',
129 'title' => $this->randomName(200),
130 ));
131
132 // Display node creation form.
133 $langcode = LANGUAGE_NONE;
134 $this->drupalGet('node/add/article');
135 $this->assertFieldByName("{$this->field_name}[$langcode][0][nid]", '', t('Widget is displayed'));
136
137 // Submit node form with autocomplete value for short title.
138 $edit = array(
139 'title' => $this->randomName(8),
140 "{$this->field_name}[$langcode][0][nid]" => $node_short_title->title . ' [nid:' . $node_short_title->nid . ']',
141 );
142 $this->drupalPost('node/add/article', $edit, t('Save'));
143 $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.'));
144 $this->assertText($node_short_title->title, t('Referenced node title is displayed.'));
145
146 // Submit node form with autocomplete value for long title.
147 $edit = array(
148 'title' => $this->randomName(8),
149 "{$this->field_name}[$langcode][0][nid]" => $node_long_title->title . ' [nid:' . $node_long_title->nid . ']',
150 );
151 $this->drupalPost('node/add/article', $edit, t('Save'));
152 $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.'));
153 $this->assertText($node_long_title->title, t('Referenced node title is displayed.'));
154 }
155 }