Browse Source

Account user permission in methods that list and view content types.

Reynold Tan 5 years ago
parent
commit
69ca3ac656
1 changed files with 13 additions and 8 deletions
  1. 13 8
      tests/tripal_ws/http/TripalWebServicesContentTest.php

+ 13 - 8
tests/tripal_ws/http/TripalWebServicesContentTest.php

@@ -12,6 +12,14 @@ class TripalWebServicesContentTest extends TripalTestCase {
 
   /** @test */
   public function testGettingMainContentList() {
+    // Grant user permission to all content.
+    $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
+    $bundles = db_query('SELECT name FROM tripal_bundle');
+    foreach($bundles as $bundle) {
+      $bundle_name = 'view ' . $bundle->name;
+      user_role_grant_permissions($role_id, array($bundle_name));
+    }
+
     $response = $this->get('web-services/content/v0.1');
 
     // Make sure it returned valid json
@@ -40,16 +48,13 @@ class TripalWebServicesContentTest extends TripalTestCase {
     // Get bundle label
     $label = db_query('SELECT label, name FROM tripal_bundle LIMIT 1')->fetchObject();
 
-    // Grant user permission to view bundle.
-    $bundle_name = 'view ' . $label->name;
-    if (!user_access($bundle_name)) {
-      // Either the user is anonymous or registered.
-      $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
-      user_role_grant_permissions($role_id, array($bundle_name));
-    }
+    // Grant user permission to this content.
+    $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
+    user_role_grant_permissions($role_id, array('view ' . $label->name));
 
     // Call /web-services/content/v0.1/[label]
-    $response = $this->get("web-services/content/v0.1/$label->label");
+    $ctype = preg_replace('/[^\w]/', '_', $label->label);
+    $response = $this->get("web-services/content/v0.1/" . $ctype);
 
     // Verify the returned JSON matches the structure
     $response->assertSuccessful();