t('[219380] db_rewrite_sql does NOT work if $query doesn\'t include "WHERE"'), 'desc' => t('The pattern that is used to split the query for injecting added where, join clause returns \'null\' if the main query does not include a where clause.'), 'group' => t('Drupal 7 Tests'), ); } // The test create a simple module with hook_db_rewrite_sql(). // Then it checks a SQL query transformation without WHERE clause and with WHERE clause. // The former fails, the second pass for now. function testIssue() { $table_name = $this->randomName(); $module_name = $this->randomName(); $module_condition = "$table_name.field_module = 'module'"; $original_condition = "$table_name.original_field = 'original'"; $module_text = <<< EOF createModule($module_name, $module_text); $query_without_where = "SELECT field FROM {$table_name}"; $query_with_where = "$query_without_where WHERE $original_condition"; $expected_regexp_query_without_where = str_replace(' ', '[\s\(\)]*?', "$query_without_where WHERE $module_condition"); @$result_without_where = db_rewrite_sql($query_without_where, $table_name); // @ avoids 'Undefined offset' notice $this->assertWantedPattern("|$expected_regexp_query_without_where|", $result_without_where, 'Check db_rewrite_sql without "where"'); $expected_regexp_query_with_where = str_replace(' ', '[\s\(\)]*?', "$query_without_where WHERE ( $module_condition AND $original_condition )"); $result_with_where = db_rewrite_sql($query_with_where, $table_name); $this->assertWantedPattern("|$expected_regexp_query_with_where|", $result_with_where, 'Check db_rewrite_sql with "where"'); $this->deleteModule($module_name); } function createModule($module_name, $module_text, $module_core = '7.x') { $module_dir = "sites/all/modules/$module_name"; $module_file = "$module_dir/$module_name.module"; $module_file_info = "$module_dir/$module_name.info"; if (file_check_directory($module_dir, FILE_CREATE_DIRECTORY)) { $info = <<< EOF name = "$module_name module" description = "$module_name module for test. Delete it when you see it." package = Temporary core = $module_core EOF; if ($fp = @fopen($module_file_info, 'w')) { fwrite($fp, $info); fclose($fp); } if ($fp = @fopen($module_file, 'w')) { fwrite($fp, $module_text); fclose($fp); } $this->drupalModuleEnable($module_name); } } function deleteModule($module_name) { $module_dir = "sites/all/modules/$module_name"; $module_file = "$module_dir/$module_name.module"; $module_file_info = "$module_dir/$module_name.info"; @unlink($module_file); @unlink($module_file_info); @rmdir($module_dir); } }