| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Data UI tests. 5 */ 6 7 require_once(drupal_get_path('module', 'data') .'/tests/data.test.inc'); 8 9 /** 10 * Test basic Data API functionality. 11 */ 12 class DataTestCaseUI extends DataTestCase { 13 14 /** 15 * Describe this test. 16 */ 17 public function getInfo() { 18 return array( 19 'name' => t('Data UI'), 20 'description' => t('Tests Data UI module\'s GUI.'), 21 'group' => t('Data'), 22 ); 23 } 24 25 /** 26 * Set up test. 27 */ 28 public function setUp() { 29 parent::setUp('ctools', 'schema', 'data', 'data_ui', 'data_ui_test'); 30 31 $this->drupalLogin( 32 $this->drupalCreateUser( 33 array( 34 'administer data tables', 35 ) 36 ) 37 ); 38 } 39 40 /** 41 * CRUD table tests on UI. 42 */ 43 public function testCRUDTable() { 44 $table_name = $this->createTable(5); 45 // @todo: edit table. 46 $this->drupalGet('admin/build/data/export/'. $table_name); 47 $this->dropTable($table_name); 48 } 49 50 /** 51 * Test change management on UI. 52 */ 53 public function testChangeManagement() { 54 55 // Check for presence of default table. 56 $this->drupalGet('admin/build/data'); 57 $this->assertText('data_table_kittens'); 58 $this->assertText('Default'); 59 $this->assertText('Override | Export'); 60 61 // Go to schema comparisons, verify that table is present and doesn't differ from 62 // schema definition. 63 $this->drupalGet('admin/build/data/compare'); 64 $this->assertText('data_table_kittens'); 65 $this->assertText('same'); 66 67 // Drop the table bypassing the API. 68 $table = data_get_table('data_table_kittens'); 69 $ret = array(); 70 db_drop_table($ret, $table->get('name')); 71 $this->assertTrue(isset($ret[0]['success']), 'Dropped table bypassing the API.'); 72 73 // Go to schema comparisons, now the table should be missing. 74 $this->drupalGet('admin/build/data/compare'); 75 $this->assertText('data_table_kittens'); 76 $this->assertText('missing - adjust'); 77 78 // Go to schema comparison of data_table_kittens. 79 $this->drupalGet('admin/build/data/compare/data_table_kittens'); 80 $this->assertText('Adjust data_table_kittens'); 81 $this->assertText('Status:'); 82 $this->assertText('missing'); 83 $this->assertText('Create a new table from schema information.'); 84 85 // Create table. 86 $this->drupalPost('admin/build/data/compare/data_table_kittens', array(), t('Create table')); 87 $this->assertText('Created table data_table_kittens'); 88 $this->assertTrue(db_table_exists('data_table_kittens'), 'Table data_table_kittens exists in DB.'); 89 $schema = schema_invoke('inspect', db_prefix_tables('{data_table_kittens}')); 90 $this->assertTrue(isset($schema['data_table_kittens']), 'Schema API inspector detects table.'); 91 $this->assertTrue(!empty($table), 'Table loaded'); 92 $comp = $table->compareSchema(); 93 $this->assertEqual($comp['status'], 'same'); 94 95 // Drop the table bypassing the API. 96 $ret = array(); 97 db_drop_table($ret, $table->get('name')); 98 $this->assertTrue(isset($ret[0]['success']), 'Dropped table bypassing the API.'); 99 100 // Override table. 101 $this->drupalGet('admin/build/data'); 102 $this->assertText('Override'); 103 $edit = array( 104 'new[name]' => 'weight', 105 'new[label]' => 'Weight', 106 'new[type]' => 'int', 107 'new[unsigned]' => TRUE, 108 'new[index]' => TRUE, 109 ); 110 $this->drupalPost('admin/build/data/edit/data_table_kittens', $edit, 'Add new'); 111 // We are expecting an error here. 112 $this->assertText('Table does not exist in database'); 113 $this->assertText('Go to Compare schemas to resolve conflicts.'); 114 // Go to admin/build/data/compare and create the table again. 115 $this->drupalPost('admin/build/data/compare/data_table_kittens', array(), t('Create table')); 116 $this->assertText('Created table data_table_kittens'); 117 // Repost the new field - this should work now. 118 $this->drupalPost('admin/build/data/edit/data_table_kittens', $edit, 'Add new'); 119 $this->assertText('Added field weight'); 120 $this->assertText('Added index for field weight'); 121 // @todo: Add a new PK configuration - this does not work right now as DB layer writes one thing while schema API it reads another. 122 // $this->drupalPost('admin/build/data/edit/data_table_kittens', array('fields[weight][primary]' => TRUE), 'Save'); 123 // $this->assertText('Saved changes'); 124 $this->drupalGet('admin/build/data'); 125 $this->assertText('Overridden'); 126 $this->drupalGet('admin/build/data/compare'); 127 $this->assertText('same'); 128 129 // Drop field that we just created and try to recreate it. 130 $ret = array(); 131 db_drop_field($ret, 'data_table_kittens', 'weight'); 132 $this->assertTrue(isset($ret[0]['success']), 'Dropped weight field bypassing the API.'); 133 $this->drupalGet('admin/build/data/compare'); 134 $this->assertText('different - adjust'); 135 $this->drupalGet('admin/build/data/compare/data_table_kittens'); 136 $this->assertText('Status:'); 137 $this->assertText('different'); 138 $this->assertText('Reasons:'); 139 $this->assertText('weight: not in database'); 140 $this->assertText('indexes weight: missing in database'); 141 // First try to alter table. 142 $this->drupalPost('admin/build/data/compare/data_table_kittens', array(), 'Alter table'); 143 $this->assertText('Resolved'); 144 $this->assertText('weight: not in database'); 145 $this->assertText('indexes weight: missing in database'); 146 $this->assertText('same'); 147 // Drop field again and now try to adjust schema info about table. 148 $ret = array(); 149 db_drop_field($ret, 'data_table_kittens', 'weight'); 150 $this->assertTrue(isset($ret[0]['success']), 'Dropped weight field bypassing the API.'); 151 $this->drupalPost('admin/build/data/compare/data_table_kittens', array(), 'Update schema information'); 152 $this->assertText('Updated schema for data_table_kittens'); 153 $this->assertText('same'); 154 } 155 156 /** 157 * Create a table. 158 */ 159 protected function createTable($num_fields = 5) { 160 $table_name = $this->randomName(); 161 $edit = array( 162 'name' => $table_name, 163 'title' => 'My table', 164 'field_num' => $num_fields 165 ); 166 $this->drupalPost('admin/build/data/create', $edit, 'Next'); 167 $this->assertText('Define the fields of the new table.'); 168 169 $fields = $this->randomFields($num_fields); 170 $edit = $this->formatEditFields($fields); 171 $this->drupalPost(NULL, $edit, 'Create'); 172 $this->assertText('Created table '. $table_name); 173 174 // Test schema in DB. 175 // @todo: why do we need to clear the cache here? 176 if ($schema = drupal_get_schema($table_name, true)) { 177 foreach ($schema['primary key'] as $key) { 178 if (is_array($key)) { 179 $primary_keys[] = $key[0]; 180 } 181 else { 182 $primary_keys[] = $key; 183 } 184 } 185 foreach ($schema['fields'] as $field_name => $field) { 186 $this->assertEqual($fields[$field_name]['type'], $field['type'], "Field $field_name has correct type."); 187 if ($field['type'] == 'int') { 188 $this->assertEqual(isset($fields[$field_name]['unsigned']), !empty($field['unsigned']) , "Field $field_name has correct unsigned value."); 189 } 190 } 191 foreach ($fields as $field_name => $config) { 192 if (isset($config['index'])) { 193 $this->assertTrue(isset($schema['indexes'][$field_name]), "Field $field_name indexed."); 194 } 195 if (isset($config['primary'])) { 196 $this->assertTrue(in_array($field_name, $primary_keys), "Field $field_name in primary key."); 197 } 198 } 199 } 200 else { 201 $this->fail('Could not create schema - invalid schema definition?'); 202 } 203 204 $this->assertTrue(db_table_exists($table_name), 'Table '. $table_name .' exists in database.'); 205 206 return $table_name; 207 } 208 209 /** 210 * Drop a table. 211 */ 212 protected function dropTable($table_name) { 213 $this->drupalPost('admin/build/data/drop/'. $table_name, array(), 'Drop'); 214 $exists = db_result(db_query('SELECT name FROM {data_tables} WHERE name = "%s"', $table_name)); 215 $this->assertFalse($exists, 'Table removed from data_tables table.'); 216 $this->assertFalse(drupal_get_schema($table_name, true), 'Table '. $table_name .' removed from schema API.'); 217 $this->assertFalse(db_table_exists($table_name), 'Table '. $table_name .' removed from DB.'); 218 } 219 220 /** 221 * Format an edit array from the result of randomFields(). 222 */ 223 protected function formatEditFields($fields) { 224 $edit = array(); 225 $fields = array_values($fields); 226 foreach ($fields as $i => $field) { 227 foreach ($field as $k => $v) { 228 $edit["fields[field_$i][$k]"] = $v; 229 } 230 } 231 return $edit; 232 } 233 234 /** 235 * Generate N random fields. Will create at least 1 field. 236 */ 237 protected function randomFields($n = 5) { 238 $fields = array(); 239 $excluded_types = array(); 240 241 for ($i = 0; $i < $n-1; $i++) { 242 $label = $this->uniqueRandomName(); 243 $name = data_safe_name($label); 244 245 // Get a random type. Make sure that serial does not occur twice. 246 $type = $this->randomValue(data_get_field_types(), $excluded_types); 247 if ($type == 'serial') { 248 $excluded_types['serial'] = 'serial'; 249 } 250 251 if (!empty($type)) { 252 $fields[$name] = array( 253 'name' => $name, 254 'label' => $label, 255 'type' => $type, 256 ); 257 if (rand(0, 1)) { 258 $fields[$name]['unsigned'] = 1; 259 } 260 // Auto increment fields must be indexed. 261 if (($fields[$name]['type'] == 'serial') || rand(0, 1)) { 262 $fields[$name]['index'] = 1; 263 } 264 if (rand(0, 1)) { 265 $fields[$name]['primary'] = 1; 266 } 267 } 268 } 269 // Make sure we have at least one field that is text, PK and indexed. 270 $name = $this->uniqueRandomName(); 271 $fields[data_safe_name($name)] = array( 272 'name' => data_safe_name($name), 273 'label' => $name, 274 'type' => 'text', 275 'index' => 1, 276 'primary' => 1, 277 ); 278 279 return $fields; 280 } 281 282 /** 283 * Get a random value from the given array. 284 * 285 * @param $array 286 * Array to pick random value from. 287 * @param $exclude 288 * Values not to pick. 289 * 290 * @return 291 * A random value from $array that does not exist in $exclude. 292 */ 293 protected function randomValue($array, $exclude = array()) { 294 $array = array_diff(array_values($array), $exclude); 295 if (empty($array)) { 296 $this->error('Array for random value selection is empty.'); 297 } 298 return $array[rand(0, count($array) - 1)]; 299 } 300 301 /** 302 * Create a _unique_ random name. 303 */ 304 protected function uniqueRandomName() { 305 static $names; 306 do { 307 $name = $this->randomName(); 308 } 309 while (isset($names[$name])); 310 $names[$name] = $name; 311 return $name; 312 } 313 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |