diff --git a/dbtng_example/dbtng_example.module b/dbtng_example/dbtng_example.module index 09bfe89..99e0f77 100644 --- a/dbtng_example/dbtng_example.module +++ b/dbtng_example/dbtng_example.module @@ -1,4 +1,5 @@ fields($entry) - ->execute(); + $return_value = db_insert('dbtng_example')->fields($entry)->execute(); } - catch (Exception $e) { + catch(Exception$e) { drupal_set_message(t('db_insert failed. Message = %message, query= %query', - array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error'); + array('%message' => $e->getMessage(), '%query' => $e->query_string) + ), 'error'); } return $return_value; } @@ -131,14 +131,12 @@ function dbtng_example_entry_insert($entry) { function dbtng_example_entry_update($entry) { try { // db_update()...->execute() returns the number of rows updated. - $count = db_update('dbtng_example') - ->fields($entry) - ->condition('pid', $entry['pid']) - ->execute(); + $count = db_update('dbtng_example')->fields($entry)->condition('pid', $entry['pid'])->execute(); } - catch (Exception $e) { + catch(Exception$e) { drupal_set_message(t('db_update failed. Message = %message, query= %query', - array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error'); + array('%message' => $e->getMessage(), '%query' => $e->query_string) + ), 'error'); } return $count; } @@ -159,13 +157,9 @@ function dbtng_example_entry_update($entry) { * @see db_delete() */ function dbtng_example_entry_delete($entry) { - db_delete('dbtng_example') - ->condition('pid', $entry['pid']) - ->execute(); - + db_delete('dbtng_example')->condition('pid', $entry['pid'])->execute(); } - /** * Read from the database using a filter array. * @@ -333,7 +327,7 @@ function dbtng_example_help($path) { break; case 'examples/dbtng/advanced': - $output = t('A more complex list of entries in the database.') . ' '; + $output = t('A more complex list of entries in the database.') . ' '; $output .= t('Only the entries with name = "John" and age older than 18 years are shown, the username of the person who created the entry is also shown.'); break; @@ -432,27 +426,27 @@ function dbtng_example_form_add($form, &$form_state) { $form = array(); $form['add'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => t('Add a person entry'), ); $form['add']['name'] = array( - '#type' => 'textfield', + '#type' => 'textfield', '#title' => t('Name'), - '#size' => 15, + '#size' => 15, ); $form['add']['surname'] = array( - '#type' => 'textfield', + '#type' => 'textfield', '#title' => t('Surname'), - '#size' => 15, + '#size' => 15, ); $form['add']['age'] = array( - '#type' => 'textfield', + '#type' => 'textfield', '#title' => t('Age'), - '#size' => 5, + '#size' => 5, '#description' => t("Values greater than 127 will cause an exception. Try it - it's a great example why exception handling is needed with DTBNG."), ); $form['add']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => t('Add'), ); @@ -467,10 +461,10 @@ function dbtng_example_form_add_submit($form, &$form_state) { // Save the submitted entry. $entry = array( - 'name' => $form_state['values']['name'], + 'name' => $form_state['values']['name'], 'surname' => $form_state['values']['surname'], - 'age' => $form_state['values']['age'], - 'uid' => $user->uid, + 'age' => $form_state['values']['age'], + 'uid' => $user->uid, ); $return = dbtng_example_entry_insert($entry); if ($return) { @@ -582,7 +576,8 @@ function dbtng_example_form_update_submit($form, &$form_state) { ); $count = dbtng_example_entry_update($entry); drupal_set_message(t("Updated entry @entry (@count row updated)", - array('@count' => $count, '@entry' => print_r($entry, TRUE)))); + array('@count' => $count, '@entry' => print_r($entry, TRUE)) + )); } /** @@ -602,7 +597,7 @@ function dbtng_example_grouping_list() { function dbtng_example_execute_group_by_select_query() { $select = db_select('dbtng_example', 'ex'); - // Select these specific fields for the output. + // Select these specific fields for the output. $select->fields('ex', array('name')); // Count('name') how many times same name comes in table . $select->addExpression('COUNT(ex.name)', 'count'); @@ -634,6 +629,8 @@ function dbtng_example_convert_resultset_to_table_render_array($rows = array()) $output = theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No records found'))); return $output; } + /** * @} End of "defgroup dbtng_example". */ + diff --git a/dbtng_example/dbtng_example.test b/dbtng_example/dbtng_example.test index d45d241..140d537 100644 --- a/dbtng_example/dbtng_example.test +++ b/dbtng_example/dbtng_example.test @@ -1,4 +1,5 @@ 'DBTNG example unit and UI tests', - 'description' => 'Various unit tests on the dbtng example module.' , + 'description' => 'Various unit tests on the dbtng example module.', 'group' => 'Examples', ); } @@ -41,7 +42,6 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { ); } - /** * Test the UI. */ @@ -54,7 +54,7 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { // Add the new entry. $this->drupalPost('examples/dbtng/add', array( - 'name' => 'Some', + 'name' => 'Some', 'surname' => 'Anonymous', 'age' => 33, ), @@ -87,20 +87,20 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { $this->assertEqual(count($rows), 1, "One row found in advanced view"); $this->assertFieldByXPath("//*[@id='block-system-main']/div/table[1]/tbody/tr/td[4]", "Roe", "Name 'Roe' Exists in advanced list"); - //Test the grouping tab - $this->drupalGet('examples/dbtng/grouping_list'); - $this->assertPattern("/John[td\/<>\w]+2/", "Text 'John' found in table with 2 count"); - //Now add new entry - $this->drupalPost('examples/dbtng/add', + //Test the grouping tab + $this->drupalGet('examples/dbtng/grouping_list'); + $this->assertPattern("/John[td\/<>\w]+2/", "Text 'John' found in table with 2 count"); + //Now add new entry + $this->drupalPost('examples/dbtng/add', array( - 'name' => 'Some', + 'name' => 'Some', 'surname' => 'Anonymous', 'age' => 31, ), t('Add') ); - $this->drupalGet('examples/dbtng/grouping_list'); - $this->assertPattern("/Some[td\/<>\w]+1/", "Text 'Some' found in table with 1 count"); + $this->drupalGet('examples/dbtng/grouping_list'); + $this->assertPattern("/Some[td\/<>\w]+1/", "Text 'Some' found in table with 1 count"); } /** @@ -144,7 +144,8 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { $this->assertEqual( count($result), 1, - 'Found one entry in the table with surname "NotDoe'); + 'Found one entry in the table with surname "NotDoe' + ); // Our NotDoe will be changed to "NowDoe". $entry = $result[0]; $entry->surname = "NowDoe"; @@ -154,7 +155,8 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { $this->assertEqual( count($result), 1, - "Found renamed 'NowDoe' surname"); + "Found renamed 'NowDoe' surname" + ); // Read only John Doe entry. $result = dbtng_example_entry_load(array('name' => 'John', 'surname' => 'Doe')); @@ -204,3 +206,4 @@ class DBTNGExampleUnitTestCase extends DrupalWebTestCase { ); } } +