| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Data module 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 DataTestCaseAPI extends DataTestCase { 13 14 /** 15 * Describe this test. 16 */ 17 public function getInfo() { 18 return array( 19 'name' => t('Data API'), 20 'description' => t('Unit tests for Data module\'s API. Turn off views module in your local site to run these tests.'), 21 'group' => t('Data'), 22 ); 23 } 24 25 /** 26 * Set up test. 27 */ 28 public function setUp() { 29 parent::setUp('ctools', 'data'); 30 } 31 32 /** 33 * Run CRUD tests. 34 */ 35 public function testCRUD() { 36 // Create table. 37 $table_name = data_name($this->randomName(5, 'crud')); 38 if (!$table = data_create_table($table_name, $this->testSchema())) { 39 $this->error('Could not create table.'); 40 return; 41 } 42 else { 43 $this->assertTrue(db_table_exists($table->get('name')), 'Created table '. $table->get('name')); 44 $schema = drupal_get_schema($table->get('name')); 45 $this->assertTrue(!empty($schema), 'Schema information is available.'); 46 } 47 48 // Save data. 49 $handler = data_get_handler($table->get('name')); 50 $test_data = $this->testData(); 51 $handler->save($test_data[0], array('id')); 52 $handler->save($test_data[1], array('id')); 53 $handler->save($test_data[1], array('id')); 54 55 // Load second record. 56 $data = $handler->load(array('id' => 1)); 57 $this->assertEqual($data[0], $test_data[1], 'Loaded data matches saved data.'); 58 59 // Manipulate second record, save and load. 60 $data[0]['char0'] = 'test'; 61 $handler->save($data[0], array('id')); 62 $data = $handler->load(array('id' => 1)); 63 $this->assertEqual($data[0]['char0'], 'test', 'Saved data matches changed data.'); 64 65 // Manipulate second record, update and load. 66 $data[0]['char0'] = 'test_update'; 67 $handler->update($data[0], array('id')); 68 $data = $handler->load(array('id' => 1)); 69 $this->assertEqual($data[0]['char0'], 'test_update', 'Updated data matches changed data.'); 70 71 // Delete first record and insert it again. 72 $data = $handler->load(array('id' => 0)); 73 $handler->delete(array('id' => 0)); 74 $this->assertFalse($handler->load(array('id' => 0)), 'Data deleted.'); 75 $handler->insert($data[0]); 76 $data = $handler->load(array('id' => 0)); 77 $this->assertEqual($data[0]['char0'], 'test00', 'Inserted data matches.'); 78 79 // Load a record by string. 80 $data = $handler->load(array('char0' => 'test_update')); 81 $this->assertEqual($data[0]['id'], 1, 'Loaded data by string type key.'); 82 83 // Delete table. 84 $table->drop($table_name); 85 $this->assertFalse(db_table_exists($table_name), 'Dropped table.'); 86 87 // Create table and drop it again. 88 $table = data_create_table($table_name, $this->testSchema()); 89 $this->assertTrue(!empty($table), 'Created table with same name '. $table_name); 90 91 // Delete table. 92 $table->drop($table_name); 93 $this->assertFalse(db_table_exists($table_name), 'Dropped table.'); 94 } 95 96 /** 97 * Test API functions of DataTable and DataHandler. 98 */ 99 public function testAPIFunctions() { 100 101 // Test data_create_table() API function. 102 $tablename = data_name($this->randomName(5, 'apifunc')); 103 $table = data_create_table($tablename, $this->testSchema()); 104 $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}")); 105 $this->assertTrue($num_of_tables == 1, "{data_create_table}: Exactly one table is created"); 106 $db_tablename = db_result(db_query("SELECT name FROM {data_tables}")); 107 $result = db_query("SELECT * FROM {%s}", $db_tablename); 108 $this->assertTrue($result != FALSE, "{data_create_table}: The table exists in the database"); 109 110 // Test data_get_table() API function. 111 $this->assertFalse(data_get_table(''), "{data_get_table}: Empty named table does not exist"); 112 $this->assertFalse(data_get_table($tablename . $this->randomName(5, 'apifunc')), "{data_get_table}: Non-existing named table does not exist"); 113 $table = data_get_table($tablename); 114 $this->assertTrue($table instanceof DataTable, "{data_get_table}: A DataTable object is returned by getting an existing table."); 115 116 // Test data_drop_table() API function 117 data_drop_table(''); 118 data_drop_table('%'); 119 data_drop_table('.'); 120 data_drop_table('\%'); 121 $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}")); 122 $this->assertTrue($num_of_tables == 1, "{data_drop_table}: It's not possible to delete tables with special (non-existing) table names."); 123 data_drop_table($tablename); 124 $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}")); 125 $this->assertTrue($num_of_tables == 0, "{data_drop_table}: The table is destroyed."); 126 $this->assertFalse(db_table_exists($db_tablename), "{data_drop_table}: The table does not exist in the database"); 127 128 // Test data_get_all_tables() API function. 129 $start = count(data_get_all_tables(TRUE)); 130 for ($i = 0; $i < 5; $i++) { 131 $name = data_name($this->randomName(20, 'apifunc')); 132 if (!data_create_table($name, $this->testSchema())) { 133 $this->fail('Could not create table.'); 134 } 135 } 136 $tables = data_get_all_tables(TRUE); 137 $this->assertTrue(count($tables) == ($start + $i), "{data_get_all_tables}: Proper number of table entries are returned."); 138 139 // Test data_export() API function. 140 if (module_exists('ctools')) { 141 $table = array_pop(data_get_all_tables()); 142 $exported = data_export($table->get('name')); 143 $this->assertTrue(strstr($exported, 'array'), "{data_export}: The schema has been exported"); 144 } 145 else { 146 $msg = data_export('foo'); 147 $this->assertEqual($msg, 'Export requires CTools http://drupal.org/project/ctools', 'Notification message is appeared'); 148 } 149 150 // Test DataTable::get(). 151 $table = array_pop(data_get_all_tables()); 152 $result = $table->get('nonexistingproperty'); 153 $this->assertTrue(empty($result), "DataTable::get(): Non-existing property does not return anything."); 154 $result = $table->get('name'); 155 $this->assertTrue(!empty($result), "DataTable::get(): Existing property returns non-empty value."); 156 157 // Test DataTable::addField(). 158 // Note: this test causes a notice if views is enabled on the system that 159 // runs the test. 160 $name = 'newfield'; // See data.test.inc and the testSchema(). It's safe to use hard-coded field name. 161 $spec = array( 162 'type' => 'int', 163 'unsigned' => TRUE, 164 'not null' => TRUE, 165 ); 166 $return = $table->addField($name, $spec); 167 $this->assertEqual($name, $return, "DataTable::addField(): Returned correct field name."); 168 $result = db_query("SELECT %s FROM {%s}", $name, $table->get('name')); 169 $this->assertTrue($result != FALSE, "DataTable::addField(): The new column exists in the database"); 170 171 $table->dropField($name); 172 // This query will cause an error, suppress it. 173 @$result = db_query("SELECT %s FROM {%s}", $name, $table->get('name')); 174 $this->assertFalse($result, "DataTable::dropField(): The column is dropped"); 175 176 // Test DataHandler::save(), DataHandler::truncate() and DataHandler::delete(). 177 $test_data = $this->testData(); 178 $table->handler()->save($test_data[0], array('id')); 179 $num_of_rows_before = db_result(db_query("SELECT COUNT(*) FROM {%s}", $table->get('name'))); 180 $table->handler()->truncate(); 181 $num_of_rows_after = db_result(db_query("SELECT COUNT(*) FROM {%s}", $table->get('name'))); 182 $this->assertEqual($num_of_rows_before, 1, "DataTable::truncate(): One row is in the table before executing it"); 183 $this->assertEqual($num_of_rows_after, 0, "DataTable::truncate(): The table is empty after executing it."); 184 185 $test_data = $this->testData(); 186 $table->handler()->save($test_data[0], array('id')); 187 $table->handler()->save($test_data[1], array('id')); 188 $table->handler()->delete(array('id' => $test_data[0]['id'])); 189 $count = db_result(db_query("SELECT COUNT(*) FROM {%s} WHERE id = '%d'", $table->get('name'), $test_data[0]['id'])); 190 $this->assertEqual($count, 0, 'The given entry is deleted'); 191 $count = db_result(db_query("SELECT COUNT(*) FROM {%s} WHERE id = '%d'", $table->get('name'), $test_data[1]['id'])); 192 $this->assertEqual($count, 1, 'The other entry is still there.'); 193 194 // Test DataTable::link(). 195 // @todo: test removing/adding a link. 196 $meta_before = $table->get('meta'); 197 $table->link('node', 'nid'); 198 $meta_after = $table->get('meta'); 199 $this->assertTrue(empty($meta_before), "DataTable::link(): The meta is empty before executing it"); 200 $this->assertTrue(!empty($meta_after), "DataTable::link(): The meta is not empty after executing it"); 201 } 202 }
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 |