1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Tests for nodereference_autocreate. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Todo: Convert this to unit test after next simpltest backport from 7 |
---|
10 | */ |
---|
11 | class NodeReferenceAutocreateUnitTestCase extends DrupalWebTestCase { |
---|
12 | |
---|
13 | public static function getInfo() { |
---|
14 | return array( |
---|
15 | 'name' => 'Node Reference Autocreate Unit Tests', |
---|
16 | 'description' => '', |
---|
17 | 'group' => 'CCK', |
---|
18 | ); |
---|
19 | } |
---|
20 | |
---|
21 | public function setUp() { |
---|
22 | parent::setUp('content', 'nodereference', 'nodereference_autocreate'); |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Test that hook_widget_info() returns the expected widget array. |
---|
27 | */ |
---|
28 | public function testHookWidgetInfo() { |
---|
29 | $expected = array( |
---|
30 | 'nodereference_autocreate' => array( |
---|
31 | 'label' => t('Auto-create node'), |
---|
32 | 'field types' => array('nodereference'), |
---|
33 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
34 | 'callbacks' => array( |
---|
35 | 'default value' => CONTENT_CALLBACK_DEFAULT, |
---|
36 | ), |
---|
37 | ), |
---|
38 | ); |
---|
39 | $this->assertEqual(nodereference_autocreate_widget_info(), $expected, 'hook_widget_info() is correct'); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Base function test. |
---|
45 | */ |
---|
46 | class NodeReferenceAutocreateFunctionalBaseTestCase extends DrupalWebTestCase { |
---|
47 | |
---|
48 | protected $user; |
---|
49 | protected $type; |
---|
50 | protected $field; |
---|
51 | |
---|
52 | /** |
---|
53 | * Create a new content type and add a nodereferences_autocreate field. |
---|
54 | */ |
---|
55 | public function createContentType() { |
---|
56 | // Create content type. |
---|
57 | $this->type = array( |
---|
58 | 'name' => $this->randomName(), |
---|
59 | 'type' => strtolower($this->randomName()), |
---|
60 | 'description' => $this->randomName(), |
---|
61 | ); |
---|
62 | $this->drupalPost('admin/content/types/add', $this->type, 'Save content type'); |
---|
63 | $this->assertText("The content type {$this->type['name']} has been added.", 'Content type save successful'); |
---|
64 | |
---|
65 | $this->type['url'] = 'admin/content/node-type/' . str_replace('_', '-', $this->type['type']); |
---|
66 | |
---|
67 | // Add field to content type. |
---|
68 | $this->field = array( |
---|
69 | '_add_new_field[label]' => $this->randomName(), |
---|
70 | '_add_new_field[field_name]' => strtolower($this->randomName()), |
---|
71 | '_add_new_field[type]' => 'nodereference', |
---|
72 | '_add_new_field[widget_type]' => 'nodereference_autocreate', |
---|
73 | ); |
---|
74 | $this->drupalPost($this->type['url'] . '/fields', $this->field, 'Save'); |
---|
75 | |
---|
76 | // Attempt to select multiple referenceable types. |
---|
77 | $edit = array( |
---|
78 | 'multiple' => 1, // Unlimited. |
---|
79 | 'referenceable_types[page]' => TRUE, |
---|
80 | 'referenceable_types[' . $this->type['type'] . ']' => TRUE, |
---|
81 | ); |
---|
82 | $this->drupalPost(NULL, $edit, 'Save field settings'); |
---|
83 | $this->assertText('Only one content type can be referenced for widgets of type Auto-create node.', 'Only one referenced type allowed.'); |
---|
84 | |
---|
85 | // Only select a single referenceable type and save field. |
---|
86 | $edit = array( |
---|
87 | 'multiple' => 1, // Unlimited. |
---|
88 | 'referenceable_types[page]' => FALSE, |
---|
89 | 'referenceable_types[' . $this->type['type'] . ']' => TRUE, |
---|
90 | ); |
---|
91 | $this->drupalPost(NULL, $edit, 'Save field settings'); |
---|
92 | $this->assertText('Added field ' . $this->field['_add_new_field[label]'] . '.', 'Field save successful'); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Check that save and update operations work as expected. A node should be |
---|
97 | * created with specified title and upon change it should be updated. |
---|
98 | */ |
---|
99 | public function checkAutoCreate() { |
---|
100 | $pre_max_nid = db_result(db_query('SELECT MAX(nid) FROM {node}')); |
---|
101 | |
---|
102 | $field_name = 'field_' . $this->field['_add_new_field[field_name]'] . '[0][nid][nid]'; |
---|
103 | $edit = array( |
---|
104 | 'title' => $this->randomName(), |
---|
105 | 'body' => $this->randomName(80), |
---|
106 | $field_name => $this->randomName(), |
---|
107 | ); |
---|
108 | $this->drupalPost('node/add/' . str_replace('_', '-', $this->type['type']), $edit, 'Save'); |
---|
109 | |
---|
110 | $post_max_nid = db_result(db_query('SELECT MAX(nid) FROM {node}')); |
---|
111 | |
---|
112 | // Compare the pre and post max nid to check for node creation. |
---|
113 | $this->assertTrue($post_max_nid > $pre_max_nid, 'Node created'); |
---|
114 | |
---|
115 | // Find link to referenced node. |
---|
116 | $title = $edit[$field_name]; |
---|
117 | $link = $this->xpath('//a[text()="' . $title . '"]'); |
---|
118 | if ($this->assertTrue($link, 'Found link to sub node')) { |
---|
119 | $link = $link[0]; |
---|
120 | |
---|
121 | // The link should be in the form 'node/#', grab the nid from the end and |
---|
122 | // load the node object. |
---|
123 | $parts = explode('/', $link['href']); |
---|
124 | $node = node_load(array_pop($parts)); |
---|
125 | |
---|
126 | $this->assertEqual($node->title, $title, 'Node title matches'); |
---|
127 | |
---|
128 | // Get the NID of the parent node. |
---|
129 | $parts = explode('/', $this->getUrl()); |
---|
130 | $nid = array_pop($parts); |
---|
131 | |
---|
132 | // Ensure that the title of the node reference is displayed without [nid:]. |
---|
133 | $this->drupalGet("node/$nid/edit"); |
---|
134 | $this->assertFieldByName($field_name, $node->title, 'Node reference field contains only title'); |
---|
135 | |
---|
136 | // Update the referenced node's title through the parent node and check |
---|
137 | // that it is updated. |
---|
138 | $edit = array( |
---|
139 | $field_name => $this->randomName(), |
---|
140 | ); |
---|
141 | $this->drupalPost(NULL, $edit, 'Save'); |
---|
142 | |
---|
143 | $this->drupalGet("node/$nid/edit"); |
---|
144 | $this->assertFieldByName($field_name, $edit[$field_name], 'Node reference field contains modified title'); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Function test implementation. |
---|
151 | */ |
---|
152 | class NodeReferenceAutocreateFunctionalTestCase extends NodeReferenceAutocreateFunctionalBaseTestCase { |
---|
153 | |
---|
154 | public static function getInfo() { |
---|
155 | return array( |
---|
156 | 'name' => 'Node Reference Autocreate Functional Tests', |
---|
157 | 'description' => '', |
---|
158 | 'group' => 'CCK', |
---|
159 | ); |
---|
160 | } |
---|
161 | |
---|
162 | public function setUp() { |
---|
163 | parent::setUp('content', 'text', 'optionwidgets', 'nodereference', 'nodereference_autocreate'); |
---|
164 | $required_permissions = array( |
---|
165 | 'administer content types', |
---|
166 | 'access content', |
---|
167 | 'administer nodes', |
---|
168 | 'access administration pages' |
---|
169 | ); |
---|
170 | $this->user = $this->drupalCreateUser($required_permissions); |
---|
171 | $this->drupalLogin($this->user); |
---|
172 | } |
---|
173 | |
---|
174 | public function testAddContentType() { |
---|
175 | $this->createContentType(); |
---|
176 | $this->checkAutoCreate(); |
---|
177 | } |
---|
178 | } |
---|