You are here

function NotificationsBasicTests::testNotificationsQueryBuilder in Notifications 6

Same name and namespace in other branches
  1. 6.4 tests/notifications_api.test \NotificationsBasicTests::testNotificationsQueryBuilder()
  2. 6.2 tests/notifications_api.test \NotificationsBasicTests::testNotificationsQueryBuilder()
  3. 6.3 tests/notifications_api.test \NotificationsBasicTests::testNotificationsQueryBuilder()
  4. 7 tests/notifications_api.test \NotificationsBasicTests::testNotificationsQueryBuilder()

Test query builder

File

tests/notifications_api.test, line 102

Class

NotificationsBasicTests
Class for testing notifications module. Tests basic API functions

Code

function testNotificationsQueryBuilder() {

  // Test query builder, first basic query, then add some fields
  $query = notifications_query_build(array(
    'select' => 'field1',
    'from' => 'table1',
    'join' => 'JOIN table2',
  ));
  list($sql, $args) = notifications_query_sql($query);
  $this
    ->assertEqual($sql, 'SELECT field1 FROM table1 JOIN table2', 'Build basic query with SELECT and JOIN.');
  $fields = array(
    'f1' => 1,
    'f2' => 'value2',
  );
  $query = notifications_query_build(array(
    'fields' => $fields,
  ), $query);
  list($sql, $args) = notifications_query_sql($query);
  $fields_sql = "(f.field = '%s' AND f.value = '%s') OR (f.field = '%s' AND f.value = '%s')";
  $target = "SELECT field1 FROM table1 JOIN table2 WHERE ({$fields_sql})";
  $this
    ->assertEqual($sql, $target, 'Build basic query with simple fields.' . $sql);
  $this
    ->assertEqual($args, array(
    'f1',
    1,
    'f2',
    'value2',
  ), 'Arguments for basic query with simple fields.');
  $fields = array(
    'f3' => array(
      1,
      2,
    ),
    'f4' => array(
      'value3',
      'value4',
    ),
  );
  $query = notifications_query_build(array(
    'fields' => $fields,
  ), $query);
  list($sql, $args) = notifications_query_sql($query);
  $fields_sql .= " OR (f.field = '%s' AND f.value IN ('%s','%s'))";
  $fields_sql .= " OR (f.field = '%s' AND f.value IN ('%s','%s'))";
  $target = "SELECT field1 FROM table1 JOIN table2 WHERE ({$fields_sql})";
  $target_args = array(
    'f1',
    1,
    'f2',
    'value2',
    'f3',
    1,
    2,
    'f4',
    'value3',
    'value4',
  );
  $this
    ->assertEqual($sql, $target, 'Build basic query with array fields, conditions match.');
  $this
    ->assertEqual($args, $target_args, 'Build basic query with array fields, arguments match.');

  // Test update/done/delete queries with two fake rows
  foreach (array(
    1,
    2,
  ) as $i) {
    db_query("INSERT INTO {notifications_queue}(eid, sid, uid, type, send_interval, send_method, sent, cron) VALUES(%d, %d, %d, 'test', 0 , 'test', 0, 1)", $i, $i, $i);
  }

  // We should have two rows, try a few things with them
  $this
    ->assertEqual($this
    ->countQueued(), 2, 'We have two rows in queue');
  variable_set('notifications_log', 1);
  notifications_queue_done(array(
    'type' => 'test',
  ));
  $this
    ->assertEqual($this
    ->countQueued(array(
    'cron' => 0,
  )), 2, 'Both rows have been marked as done');
  variable_del('notifications_log');
  notifications_queue_done(array(
    'type' => 'test',
  ));
  $this
    ->assertEqual($this
    ->countQueued(array(
    'cron' => 0,
  )), 0, 'Both rows have been deleted');
}