123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Tests\tripal_ws\http;
- use StatonLab\TripalTestSuite\DBTransaction;
- use StatonLab\TripalTestSuite\TripalTestCase;
- class TripalWebServicesContentTest extends TripalTestCase {
-
- use DBTransaction;
-
- public function testGettingMainContentList() {
-
- $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');
-
- $response->assertSuccessful();
- $response->assertJsonStructure([
- '@context',
- '@id',
- '@type',
- 'label',
- 'totalItems',
- 'member' => [
- [
- '@id',
- '@type',
- 'label',
- 'description',
- ],
- ],
- ]);
- }
-
- public function testGettingListOfEntitiesInABundle() {
-
- $label = db_query('SELECT label, name FROM tripal_bundle LIMIT 1')->fetchObject();
-
- $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
- user_role_grant_permissions($role_id, array('view ' . $label->name));
-
- $ctype = preg_replace('/[^\w]/', '_', $label->label);
- $response = $this->get("web-services/content/v0.1/" . $ctype);
-
- $response->assertSuccessful();
- $response->assertJsonStructure([
- '@context',
- '@id',
- '@type',
- 'label',
- 'totalItems',
- 'member',
- ]);
-
- $json = $response->json();
- $this->assertEquals($json['label'], "$label->label Collection");
- }
-
- public function testSanitizeFieldKeys() {
-
- $label = db_query('SELECT label FROM tripal_bundle LIMIT 1')->fetchField();
- $bundle = tripal_load_bundle_entity(['label' => $label]);
- $this->assertNotNull($bundle,
- "Unable to load the associated bundle object.");
-
- $base_path = "web-services/content/v0.1/$label";
- $resource = new \TripalWebServiceResource($base_path);
- $this->assertNotNull($resource,
- "Unable to create a Tripal Web Service Resource for testing.");
-
- module_load_include('inc', 'tripal_ws', 'includes/TripalWebService/TripalContentService_v0_1');
- $web_service = new \TripalContentService_v0_1($base_path);
- $this->assertNotNull($web_service,
- "Unable to create a TripalContentService_v0_1 object for testing.");
-
-
- $value = [
- 'rdfs:type' => 'fake',
- ];
- $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
- $resource,
- $value,
- $bundle,
- $base_path
- ]);
- $this->assertNotNull($sanitized_value,
- "You should be able to sanitize a term-indexed array if terms are valid.");
-
- $value = [
- 'fake',
- 'none',
- 5
- ];
- $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
- $resource,
- $value,
- $bundle,
- $base_path
- ]);
- $this->assertNotNull($sanitized_value,
- "You should be able to sanitize a numeric-indexed array if sub-elements are direct values.");
-
- $value = [
- ['rdfs:type' => 'fake'],
- ['rdfs:type' => 'none'],
- ];
- $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
- $resource,
- $value,
- $bundle,
- $base_path
- ]);
- $this->assertNotNull($sanitized_value,
- "You should be able to sanitize a numeric-indexed array if sub-elements are also arrays.");
-
-
- $value = [
- ['randomnotterm' => 'fake'],
- ];
- $sanitized_value = $this->invokeMethod($web_service, 'sanitizeFieldKeys', [
- $resource,
- $value,
- $bundle,
- $base_path
- ]);
- $this->assertEmpty($sanitized_value[0],
- "You should be able to sanitize a numeric-indexed array if sub-elements arrays are not keyed with valid terms.");
- }
-
- public function invokeMethod(&$object, $methodName, array $parameters = array()) {
- $reflection = new \ReflectionClass(get_class($object));
- $method = $reflection->getMethod($methodName);
- $method->setAccessible(true);
- return $method->invokeArgs($object, $parameters);
- }
- }
|