TripalWebServicesContentTest.php 4.7 KB

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