TripalWebServicesContentTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Tests\tripal_ws\http;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class TripalWebServicesContentTest extends TripalTestCase {
  6. // Uncomment to auto start and rollback db transactions per test method.
  7. use DBTransaction;
  8. /** @test */
  9. public function testGettingMainContentList() {
  10. // Grant user permission to all content.
  11. $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
  12. $bundles = db_query('SELECT name FROM tripal_bundle');
  13. foreach($bundles as $bundle) {
  14. $bundle_name = 'view ' . $bundle->name;
  15. user_role_grant_permissions($role_id, array($bundle_name));
  16. }
  17. $response = $this->get('web-services/content/v0.1');
  18. // Make sure it returned valid json
  19. $response->assertSuccessful();
  20. $response->assertJsonStructure([
  21. '@context',
  22. '@id',
  23. '@type',
  24. 'label',
  25. 'totalItems',
  26. 'member' => [
  27. [
  28. '@id',
  29. '@type',
  30. 'label',
  31. 'description',
  32. ],
  33. ],
  34. ]);
  35. }
  36. /** @test
  37. * @group ws
  38. */
  39. public function testGettingListOfEntitiesInABundle() {
  40. // Get bundle label
  41. $label = db_query('SELECT label, name FROM tripal_bundle LIMIT 1')->fetchObject();
  42. // Grant user permission to this content.
  43. $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
  44. user_role_grant_permissions($role_id, array('view ' . $label->name));
  45. // Call /web-services/content/v0.1/[label]
  46. $ctype = preg_replace('/[^\w]/', '_', $label->label);
  47. $response = $this->get("web-services/content/v0.1/" . $ctype);
  48. // Verify the returned JSON matches the structure
  49. $response->assertSuccessful();
  50. $response->assertJsonStructure([
  51. '@context',
  52. '@id',
  53. '@type',
  54. 'label',
  55. 'totalItems',
  56. 'member',
  57. ]);
  58. // Verify the collection is of the correct type
  59. $json = $response->json();
  60. $this->assertEquals($json['label'], "$label->label Collection");
  61. }
  62. /**
  63. * Tests the sanitizeFieldKeys() method.
  64. * @group tripal_ws
  65. * @group tripalWS-ServiceResource
  66. */
  67. public function testSanitizeFieldKeys() {
  68. // We need a bundle in order to determine a valid base path.
  69. $label = db_query('SELECT label FROM tripal_bundle LIMIT 1')->fetchField();
  70. $bundle = tripal_load_bundle_entity(['label' => $label]);
  71. $this->assertNotNull($bundle,
  72. "Unable to load the associated bundle object.");
  73. // We need a resource to add context to.
  74. $base_path = "web-services/content/v0.1/$label";
  75. $resource = new \TripalWebServiceResource($base_path);
  76. $this->assertNotNull($resource,
  77. "Unable to create a Tripal Web Service Resource for testing.");
  78. // We need a ContentService object to call sanitizeFieldKeys().
  79. module_load_include('inc', 'tripal_ws', 'includes/TripalWebService/TripalContentService_v0_1');
  80. $web_service = new \TripalContentService_v0_1($base_path);
  81. $this->assertNotNull($web_service,
  82. "Unable to create a TripalContentService_v0_1 object for testing.");
  83. // Now finally, we try to test it!
  84. // - Associative array where keys are valid terms.
  85. $value = [
  86. 'rdfs:type' => 'fake',
  87. ];
  88. $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
  89. $resource,
  90. $value,
  91. $bundle,
  92. $base_path
  93. ]);
  94. $this->assertNotNull($sanitized_value,
  95. "You should be able to sanitize a term-indexed array if terms are valid.");
  96. // - Numeric keys to direct values.
  97. $value = [
  98. 'fake',
  99. 'none',
  100. 5
  101. ];
  102. $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
  103. $resource,
  104. $value,
  105. $bundle,
  106. $base_path
  107. ]);
  108. $this->assertNotNull($sanitized_value,
  109. "You should be able to sanitize a numeric-indexed array if sub-elements are direct values.");
  110. // - Numeric keys where value is an array with term keys.
  111. $value = [
  112. ['rdfs:type' => 'fake'],
  113. ['rdfs:type' => 'none'],
  114. ];
  115. $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
  116. $resource,
  117. $value,
  118. $bundle,
  119. $base_path
  120. ]);
  121. $this->assertNotNull($sanitized_value,
  122. "You should be able to sanitize a numeric-indexed array if sub-elements are also arrays.");
  123. // - Numeric keys where value is an array with random keys.
  124. // (random keys should be removed.)
  125. $value = [
  126. ['randomnotterm' => 'fake'],
  127. ];
  128. $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
  129. $resource,
  130. $value,
  131. $bundle,
  132. $base_path
  133. ]);
  134. $this->assertEmpty($sanitized_value[0],
  135. "You should be able to sanitize a numeric-indexed array if sub-elements arrays are not keyed with valid terms.");
  136. }
  137. /**
  138. * Call protected/private method of a class.
  139. *
  140. * @param object &$object Instantiated object that we will run method on.
  141. * @param string $methodName Method name to call
  142. * @param array $parameters Array of parameters to pass into method.
  143. *
  144. * @return mixed Method return.
  145. */
  146. public function invokeMethod(&$object, $methodName, array $parameters = array()) {
  147. $reflection = new \ReflectionClass(get_class($object));
  148. $method = $reflection->getMethod($methodName);
  149. $method->setAccessible(true);
  150. return $method->invokeArgs($object, $parameters);
  151. }
  152. }