TripalWebServicesContentTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, name FROM tripal_bundle LIMIT 1')->fetchObject();
  35. // Grant user permission to view bundle.
  36. $bundle_name = 'view ' . $label->name;
  37. if (!user_access($bundle_name)) {
  38. // Either the user is anonymous or registered.
  39. $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
  40. user_role_grant_permissions($role_id, array($bundle_name));
  41. }
  42. // Call /web-services/content/v0.1/[label]
  43. $response = $this->get("web-services/content/v0.1/$label->label");
  44. // Verify the returned JSON matches the structure
  45. $response->assertSuccessful();
  46. $response->assertJsonStructure([
  47. '@context',
  48. '@id',
  49. '@type',
  50. 'label',
  51. 'totalItems',
  52. 'member',
  53. ]);
  54. // Verify the collection is of the correct type
  55. $json = $response->json();
  56. $this->assertEquals($json['label'], "$label->label Collection");
  57. }
  58. }