TripalWebServicesContentTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }