122abdde1992d01179ae539876dfd899491a0b6d
[project/data.git] / data_ui / tests / data_ui.test
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Data UI tests.
6 */
7
8 require_once(drupal_get_path('module', 'data') .'/tests/data.test.inc');
9
10 /**
11 * Test basic Data API functionality.
12 */
13 class DataTestCaseUI extends DataTestCase {
14
15 /**
16 * Describe this test.
17 */
18 public function getInfo() {
19 return array(
20 'name' => t('Data UI'),
21 'description' => t('Tests Data UI module\'s GUI.'),
22 'group' => t('Data'),
23 );
24 }
25
26 /**
27 * Set up test.
28 */
29 public function setUp() {
30 parent::setUp('data', 'data_ui');
31
32 $this->drupalLogin(
33 $this->drupalCreateUser(
34 array(
35 'administer data tables',
36 )
37 )
38 );
39 }
40
41 /**
42 * CRUD table tests on UI.
43 */
44 public function testCRUDTable() {
45 $table_name = $this->createTable(5);
46 // @todo: edit table.
47 $this->drupalGet('admin/content/data/export/'. $table_name);
48 $this->dropTable($table_name);
49 }
50
51 /**
52 * Test change management on UI.
53 */
54 public function testChangeManagement() {
55 // Only execute if ctools and schema modules are present.
56 if (!drupal_get_filename('module', 'ctools') || !drupal_get_filename('module', 'schema')) {
57 return;
58 }
59 drupal_install_modules(array('ctools', 'schema', 'data_ui_test'));
60 menu_rebuild();
61
62 // Check for presence of default table.
63 $this->drupalGet('admin/content/data');
64 $this->assertText('data_table_kittens');
65 $this->assertText('Default');
66 $this->assertText('Override | Export');
67
68 // Go to schema comparisons.
69 $this->drupalGet('admin/content/data/compare');
70 $this->assertText('data_table_kittens');
71 $this->assertText('missing - adjust');
72 $this->error($this->content);
73
74 // Go to schema comparison of data_table_kittens.
75 $this->drupalGet('admin/content/data/compare/data_table_kittens');
76 $this->assertText('Adjust data_table_kittens');
77 $this->assertText('Status:');
78 $this->assertText('missing');
79 $this->assertText('Create a new table from schema information.');
80
81 // Create table.
82 $this->drupalPost('admin/content/data/compare/data_table_kittens', array(), t('Create table'));
83 $this->assertText('Created table data_table_kittens');
84 $schema = schema_invoke('inspect');
85 $this->assertTrue($schema['data_table_kittens']);
86 $table = data_get_table('data_table_kittens');
87 $this->assertTrue(!empty($table), 'Table loaded');
88 // $comp = $table->compareSchema();
89 // $this->assertEqual($comp['status'], 'same');
90 }
91
92 /**
93 * Create a table.
94 */
95 protected function createTable($num_fields = 5) {
96 $table_name = $this->randomName();
97 $edit = array(
98 'name' => $table_name,
99 'title' => 'My table',
100 'field_num' => $num_fields
101 );
102 $this->drupalPost('admin/content/data/create', $edit, 'Next');
103 $this->assertText('Define the fields of the new table.');
104
105 $fields = $this->randomFields($num_fields);
106 $edit = $this->formatEditFields($fields);
107 $this->drupalPost(NULL, $edit, 'Create');
108 // Data UI has prefixed the table name.
109 $table_name = data_name($table_name);
110 $this->assertText('Created table '. $table_name);
111
112 // Test schema in DB.
113 // @todo: why do we need to clear the cache here?
114 if ($schema = drupal_get_schema($table_name, true)) {
115 foreach ($schema['primary key'] as $key) {
116 if (is_array($key)) {
117 $primary_keys[] = $key[0];
118 }
119 else {
120 $primary_keys[] = $key;
121 }
122 }
123 foreach ($schema['fields'] as $field_name => $field) {
124 $this->assertEqual($fields[$field_name]['type'], $field['type'], "Field $field_name has correct type.");
125 if ($field['type'] == 'int') {
126 $this->assertEqual(isset($fields[$field_name]['unsigned']), !empty($field['unsigned']) , "Field $field_name has correct unsigned value.");
127 }
128 }
129 foreach ($fields as $field_name => $config) {
130 if (isset($config['index'])) {
131 $this->assertTrue(isset($schema['indexes'][$field_name]), "Field $field_name indexed.");
132 }
133 if (isset($config['primary'])) {
134 $this->assertTrue(in_array($field_name, $primary_keys), "Field $field_name in primary key.");
135 }
136 }
137 }
138 else {
139 $this->assertTrue(FALSE, 'Could not create schema.');
140 }
141
142 return $table_name;
143 }
144
145 /**
146 * Drop a table.
147 */
148 protected function dropTable($table_name) {
149 $this->drupalPost('admin/content/data/drop/'. $table_name, array(), 'Drop');
150 $exists = db_result(db_query('SELECT name FROM {data_tables} WHERE name = "%s"', $table_name));
151 $this->assertFalse($exists, 'Table removed from data_tables table.');
152 $this->assertFalse(drupal_get_schema($table_name, true), 'Table '. $table_name .' removed from schema API.');
153 $this->assertFalse(db_table_exists($table_name), 'Table '. $table_name .' removed from DB.');
154 }
155
156 /**
157 * Format an edit array from the result of randomFields().
158 */
159 protected function formatEditFields($fields) {
160 $edit = array();
161 $fields = array_values($fields);
162 foreach ($fields as $i => $field) {
163 foreach ($field as $k => $v) {
164 $edit["fields[field_$i][$k]"] = $v;
165 }
166 }
167 return $edit;
168 }
169
170 /**
171 * Generate N random fields. Will create at least 1 field.
172 */
173 protected function randomFields($n = 5) {
174 $fields = array();
175 for ($i = 0; $i < $n-1; $i++) {
176 $label = $this->uniqueRandomName();
177 $name = data_safe_name($label);
178 $fields[$name] = array(
179 'name' => $name,
180 'label' => $label,
181 'type' => $this->randomValue(data_get_field_types()),
182 );
183 if (rand(0, 1)) {
184 $fields[$name]['unsigned'] = 1;
185 }
186 if (rand(0, 1)) {
187 $fields[$name]['index'] = 1;
188 }
189 if (rand(0, 1)) {
190 $fields[$name]['primary'] = 1;
191 }
192 }
193 // Make sure we have at least one field that is text, PK and indexed.
194 $name = $this->uniqueRandomName();
195 $fields[data_safe_name($name)] = array(
196 'name' => data_safe_name($name),
197 'label' => $name,
198 'type' => 'text',
199 'index' => 1,
200 'primary' => 1,
201 );
202
203 return $fields;
204 }
205
206 /**
207 * Get a random value from the given array.
208 */
209 protected function randomValue($array) {
210 $array = array_values($array);
211 return $array[rand(0, count($array) - 1)];
212 }
213
214 /**
215 * Create a _unique_ random name.
216 */
217 protected function uniqueRandomName() {
218 static $names;
219 do {
220 $name = $this->randomName();
221 }
222 while (isset($names[$name]));
223 $names[$name] = $name;
224 return $name;
225 }
226 }