浏览代码

Move schema replace function to the extender class and add unit tests for the functions

Abdullah Almsaeed 6 年之前
父节点
当前提交
579d009f25

+ 25 - 0
tests/tripal_chado/api/ChadoQueryTest.php

@@ -238,4 +238,29 @@ class ChadoQueryTest extends TripalTestCase {
     $this->assertNotEmpty($found);
     $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
   }
+
+  /**
+   * @group api
+   * @group chado
+   * @group chado_db_select
+   */
+  public function test_is_chado_table_returns_correct_results() {
+    $this->assertTrue(\ChadoPrefixExtender::isChadoTable('analysis'));
+    $this->assertTrue(\ChadoPrefixExtender::isChadoTable('feature_cvtermprop'));
+    $this->assertFalse(\ChadoPrefixExtender::isChadoTable('users'));
+  }
+
+  /**
+   * @group api
+   * @group chado
+   * @group chado_db_select
+   */
+  public function test_get_real_schema_returns_correct_results() {
+    $chado = chado_get_schema_name('chado');
+    $public = chado_get_schema_name('drupal');
+
+    $this->assertEquals($chado . '.analysis', \ChadoPrefixExtender::getRealSchema('chado.analysis'));
+    $this->assertEquals($public . '.users', \ChadoPrefixExtender::getRealSchema('public.users'));
+    $this->assertEquals('users', \ChadoPrefixExtender::getRealSchema('users'));
+  }
 }

+ 0 - 22
tripal_chado/api/tripal_chado.query.api.inc

@@ -2143,25 +2143,3 @@ function chado_replace_table_prefix($string) {
 
   return $string;
 }
-
-/**
- * Replaces the schema name form public to the correct drupal schema name.
- *
- * @param string $table The table name with a prefix such as "public."
- *
- * @return mixed
- *    The table name with the correct prefix.
- */
-function chado_replace_schema_prefix($table) {
-  if (strpos($table, 'public.') === 0) {
-    $replace = chado_get_schema_name('drupal') . '.';
-    return str_replace('public.', $replace, $table);
-  }
-
-  if (strpos($table, 'chado.') === 0) {
-    $replace = chado_get_schema_name('chado') . '.';
-    return str_replace('chado.', $replace, $table);
-  }
-
-  return $table;
-}

+ 25 - 2
tripal_chado/includes/ChadoPrefixExtender.inc

@@ -180,7 +180,7 @@ class ChadoPrefixExtender extends SelectQueryExtender {
   /**
    * Checks if a table is a chado table.
    *
-   * @param string $table
+   * @param string $table The table name.
    *
    * @return bool
    */
@@ -220,7 +220,30 @@ class ChadoPrefixExtender extends SelectQueryExtender {
     // Now that the schema has been set, we can replace it with the correct
     // name. Note that schema names can be altered by developers so we need to
     // to run the following function to obtain the final name.
-    $table = chado_replace_schema_prefix($table);
+    $table = static::getRealSchema($table);
+
+    return $table;
+  }
+
+  /**
+   * Allows altered schema names to be replaces correctly.
+   *
+   * @param string $table
+   *     The table name with a prefix such as "chado." or "public."
+   *
+   * @return mixed
+   *    The table name with the correct prefix.
+   */
+  public static function getRealSchema($table) {
+    if (strpos($table, 'public.') === 0) {
+      $replace = chado_get_schema_name('drupal') . '.';
+      return str_replace('public.', $replace, $table);
+    }
+
+    if (strpos($table, 'chado.') === 0) {
+      $replace = chado_get_schema_name('chado') . '.';
+      return str_replace('chado.', $replace, $table);
+    }
 
     return $table;
   }