Browse Source

Add tripal ws RESTful tests

Abdullah Almsaeed 6 years ago
parent
commit
de05b2f906

+ 1 - 0
.travis.yml

@@ -73,6 +73,7 @@ script:
 
   # Run PHPUnit tests
   - composer update
+  - cp tests/example.env tests/.env
   - ./vendor/bin/phpunit
 
   # Test Tripal v2 to v3 upgrade steps

+ 1 - 0
tests/example.env

@@ -1,2 +1,3 @@
+BASE_URL=http://localhost
 DRUPAL_ROOT=/var/www/html
 FAKER_LOCALE=en_US

+ 64 - 0
tests/tripal_ws/http/TripalContentTest.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace Tests\tripal_ws\http;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+
+class TripalContentTest extends TripalTestCase{
+
+  // Uncomment to auto start and rollback db transactions per test method.
+  use DBTransaction;
+
+  /** @test */
+  public function testGettingMainContentList() {
+    //call /web-services/content/v0.1
+    $response = $this->get('/web-services/content/v0.1');
+
+    // Make sure it returned valid json
+    $response->assertSuccessful();
+    $response->assertJsonStructure([
+      '@context',
+      '@id',
+      '@type',
+      'label',
+      'totalItems',
+      'member' => [
+        [
+          '@id',
+          '@type',
+          'label',
+          'description',
+        ],
+      ],
+    ]);
+  }
+
+  /** @test */
+  public function testGettingListOfEntitiesInABundle() {
+    // Get bundle label
+    $label = db_query('SELECT label FROM tripal_bundle LIMIT 1')->fetchField();
+
+    // Call /web-services/content/v0.1/[label]
+    $response = $this->get("/web-services/content/v0.1/$label");
+
+    // Verify the returned JSON matches the structure
+    $response->assertSuccessful();
+    $response->assertJsonStructure([
+      '@context',
+      '@id',
+      '@type',
+      'label',
+      'totalItems',
+      'member',
+    ]);
+
+    // Verify the collection is of the correct type
+    $json = $response->json();
+    $this->assertEquals($json['label'], "$label Collection");
+  }
+
+  public function testGettingAFeatureResource() {
+    // Call /web-services/content/v0.1/mRNA/1428
+  }
+}

+ 4 - 4
tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

@@ -68,7 +68,7 @@ class TripalContentService_v0_1 extends TripalWebService {
       $ctype_lookup = array();
       $found = FALSE;
       while ($bundle = $bundles->fetchObject()) {
-        // Check the label by replacing non alpha-numeric characters with 
+        // Check the label by replacing non alpha-numeric characters with
         // an underscore and is case-insensitive
         $label = preg_replace('/[^\w]/', '_', $bundle->label);
         if (preg_match("/^$label$/i", $ctype)) {
@@ -278,7 +278,7 @@ class TripalContentService_v0_1 extends TripalWebService {
       }
       // Get the information about this field.
       $field = field_info_field($field_name);
-      
+
       // If the field has the $no_data turned on then we should exclude it.
       if (tripal_load_include_field_class($field['type'])) {
         $field_class = $field['type'];
@@ -885,8 +885,8 @@ class TripalContentService_v0_1 extends TripalWebService {
 
     // Perform the query just as a count first to get the number of records.
     $cquery = clone $query;
-    $cquery->count();
-    $num_records = $cquery->execute();
+    $num_records = $cquery->count();
+    //$num_records = $cquery->execute();
 
     if (!$num_records) {
       $num_records = 0;