Skip to content

Commit

Permalink
Issue #58: hidden/visible properties on relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Frohlich committed Oct 25, 2013
1 parent 0642070 commit a87c6e6
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 16 deletions.
102 changes: 101 additions & 1 deletion starship/RestfullYii/tests/unit/ERestJSONOutputWidgetUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,107 @@ public function testModelsToArray()
$expected = CJSON::decode(CJSON::encode($model));
$expected['author'] = CJSON::decode(CJSON::encode($model->author));
$this->assertArraysEqual($expected, $this->getWidget()->modelsToArray($model, $relations));
}
}

/**
* testPropertyIsVisableVisibleProperties
*
* tests ERestJSONOutputWidget->propertyIsVisable()
*/
public function testPropertyIsVisableVisibleProperties()
{
$widget = $this->getWidget([
'type' => 'rest',
'success' => true,
'message' => 'Record Returned',
'visibleProperties'=>['id', 'title', '*.name', 'categories.id'],
'totalCount' => 1,
'modelName' => 'Post',
'relations' => ['categories'],
'data' => Post::model()->with('categories')->findByPk(1)
]);

$this->assertTrue($widget->propertyIsVisable('id'));
$this->assertTrue($widget->propertyIsVisable('title'));
$this->assertFalse($widget->propertyIsVisable('create_time'));

$this->assertTrue($widget->propertyIsVisable('name', 'categories'));
$this->assertTrue($widget->propertyIsVisable('id', 'categories'));
$this->assertFalse($widget->propertyIsVisable('title', 'categories'));
}

/**
* testPropertyIsVisableHiddenProperties
*
* tests ERestJSONOutputWidget->propertyIsVisable()
*/
public function testPropertyIsVisableHiddenProperties()
{
$widget = $this->getWidget([
'type' => 'rest',
'success' => true,
'message' => 'Record Returned',
'hiddenProperties'=>['id', 'title', '*.name', 'categories.id'],
'totalCount' => 1,
'modelName' => 'Post',
'relations' => ['categories'],
'data' => Post::model()->with('categories')->findByPk(1)
]);

$this->assertFalse($widget->propertyIsVisable('id'));
$this->assertFalse($widget->propertyIsVisable('title'));
$this->assertTrue($widget->propertyIsVisable('create_time'));

$this->assertFalse($widget->propertyIsVisable('name', 'categories'));
$this->assertFalse($widget->propertyIsVisable('id', 'categories'));
$this->assertTrue($widget->propertyIsVisable('title', 'categories'));
}

/**
* testProcessAttributesMainModel
*
* tests ERestJSONOutputWidget->processAttributes()
*/
public function testProcessAttributesMainModel()
{
$model = Post::model()->with('categories')->findByPk(1);

$widget = $this->getWidget([
'type' => 'rest',
'success' => true,
'message' => 'Record Returned',
'totalCount' => 1,
'modelName' => 'Post',
'relations' => ['categories'],
'data' => $model
]);

$this->assertArraysEqual($model->attributes, $widget->processAttributes($model));
}


/**
* testProcessAttributesRelatedModel
*
* tests ERestJSONOutputWidget->processAttributes()
*/
public function testProcessAttributesRelatedModel()
{
$model = Post::model()->with('categories')->findByPk(1);

$widget = $this->getWidget([
'type' => 'rest',
'success' => true,
'message' => 'Record Returned',
'totalCount' => 1,
'modelName' => 'Post',
'relations' => ['categories'],
'data' => $model
]);

$this->assertArraysEqual($model->categories[0]->attributes, $widget->processAttributes($model->categories[0], 'categories'));
}



/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testGETResourceVisibleProperties()
}

/**
* testGETResourcesVisibleProperties
* testGETResourcesHiddenProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
Expand Down Expand Up @@ -81,4 +81,71 @@ public function testGETResourceHiddenProperties()
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}

/**
* testGETResourcesVisibleRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With limited selected visible fields only
* returns the correct response
*/
public function testGETResourceVisibleRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.visible.properties', function() {
return ['username', 'email', 'posts.id', '*.title', 'profile.website'];
});

$request->addEvent('model.with.relations', function() {
return ['posts', 'profile'];
});

$request['config'] = [
'url' => 'http://api/user/1',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();
$expected_response = '{"success":true,"message":"Record Found","data":{"totalCount":1,"user":{"username":"username1","email":"email@email1.com","posts":[{"id":"1","title":"title1"}],"profile":{"website":"mysite1.com"}}}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}

/**
* testGETResourcesHiddenRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
* returns the correct response
*/
public function testGETResourceHiddenRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.hidden.properties', function() {
return ['password', 'id', '*.title', 'posts.id', '*.website'];
});

$request->addEvent('model.with.relations', function() {
return ['posts', 'profile'];
});

$request['config'] = [
'url' => 'http://api/user/1',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();
$expected_response = '{"success":true,"message":"Record Found","data":{"totalCount":1,"user":{"username":"username1","email":"email@email1.com","posts":[{"content":"content1","create_time":"2013-08-07 10:09:41","author_id":"1"}],"profile":{"id":"1","user_id":"1","photo":"1"}}}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testGETResourcesVisibleProperties()
}

/**
* testGETResourcesVisibleProperties
* testGETResourcesHiddenProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
Expand Down Expand Up @@ -77,9 +77,148 @@ public function testGETResourcesHiddenProperties()
];

$request_response = $request->send();
//echo $request_response; exit();
$expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"username":"username1","email":"email@email1.com"},{"username":"username2","email":"email@email2.com"},{"username":"username3","email":"email@email3.com"},{"username":"username4","email":"email@email4.com"},{"username":"username5","email":"email@email5.com"},{"username":"username6","email":"email@email6.com"}]}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}


/**
* testGETResourcesHiddenGlobalRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
* returns the correct response
*/
public function testGETResourcesHiddenGlobalRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.hidden.properties', function() {
return ['*.photo'];
});

$request->addEvent('model.with.relations', function() {
return ['profile'];
});

$request['config'] = [
'url' => 'http://api/user',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();

$expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"id":"1","username":"username1","password":"password1","email":"email@email1.com","profile":{"id":"1","user_id":"1","website":"mysite1.com"}},{"id":"2","username":"username2","password":"password2","email":"email@email2.com","profile":{"id":"2","user_id":"2","website":"mysite2.com"}},{"id":"3","username":"username3","password":"password3","email":"email@email3.com","profile":{"id":"3","user_id":"3","website":"mysite3.com"}},{"id":"4","username":"username4","password":"password4","email":"email@email4.com","profile":{"id":"4","user_id":"4","website":"mysite4.com"}},{"id":"5","username":"username5","password":"password5","email":"email@email5.com","profile":{"id":"5","user_id":"5","website":"mysite5.com"}},{"id":"6","username":"username6","password":"password6","email":"email@email6.com","profile":{"id":"6","user_id":"6","website":"mysite6.com"}}]}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}


/**
* testGETResourcesHiddenSpecificRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
* returns the correct response
*/
public function testGETResourcesHiddenSpecificRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.hidden.properties', function() {
return ['profile.photo', 'profile.website'];
});

$request->addEvent('model.with.relations', function() {
return ['profile'];
});

$request['config'] = [
'url' => 'http://api/user',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();
$expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"id":"1","username":"username1","password":"password1","email":"email@email1.com","profile":{"id":"1","user_id":"1"}},{"id":"2","username":"username2","password":"password2","email":"email@email2.com","profile":{"id":"2","user_id":"2"}},{"id":"3","username":"username3","password":"password3","email":"email@email3.com","profile":{"id":"3","user_id":"3"}},{"id":"4","username":"username4","password":"password4","email":"email@email4.com","profile":{"id":"4","user_id":"4"}},{"id":"5","username":"username5","password":"password5","email":"email@email5.com","profile":{"id":"5","user_id":"5"}},{"id":"6","username":"username6","password":"password6","email":"email@email6.com","profile":{"id":"6","user_id":"6"}}]}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}


/**
* testGETResourcesVissibleGlobalRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
* returns the correct response
*/
public function testGETResourcesVissibleGlobalRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.visible.properties', function() {
return ['id', '*.website'];
});

$request->addEvent('model.with.relations', function() {
return ['profile'];
});

$request['config'] = [
'url' => 'http://api/user',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();
$expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"id":"1","profile":{"website":"mysite1.com"}},{"id":"2","profile":{"website":"mysite2.com"}},{"id":"3","profile":{"website":"mysite3.com"}},{"id":"4","profile":{"website":"mysite4.com"}},{"id":"5","profile":{"website":"mysite5.com"}},{"id":"6","profile":{"website":"mysite6.com"}}]}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}


/**
* testGETResourcesVisibleSpecificRelatedProperties
*
* tests that a GET request for a list of 'User' resources
* With exluded visible fields only
* returns the correct response
*/
public function testGETResourcesVisibleSpecificRelatedProperties()
{
$request = new ERestTestRequestHelper();

$request->addEvent('model.visible.properties', function() {
return ['profile.photo', 'profile.website'];
});

$request->addEvent('model.with.relations', function() {
return ['profile'];
});

$request['config'] = [
'url' => 'http://api/user',
'type' => 'GET',
'data' => null,
'headers' => [
'X_REST_USERNAME' => 'admin@restuser',
'X_REST_PASSWORD' => 'admin@Access',
],
];

$request_response = $request->send();
$expected_response = '{"success":true,"message":"Record(s) Found","data":{"totalCount":"6","user":[{"id":"1","username":"username1","password":"password1","email":"email@email1.com","profile":{"photo":"1","website":"mysite1.com"}},{"id":"2","username":"username2","password":"password2","email":"email@email2.com","profile":{"photo":"0","website":"mysite2.com"}},{"id":"3","username":"username3","password":"password3","email":"email@email3.com","profile":{"photo":"1","website":"mysite3.com"}},{"id":"4","username":"username4","password":"password4","email":"email@email4.com","profile":{"photo":"0","website":"mysite4.com"}},{"id":"5","username":"username5","password":"password5","email":"email@email5.com","profile":{"photo":"1","website":"mysite5.com"}},{"id":"6","username":"username6","password":"password6","email":"email@email6.com","profile":{"photo":"0","website":"mysite6.com"}}]}}';
$this->assertJsonStringEqualsJsonString($request_response, $expected_response);
}
}
Loading

0 comments on commit a87c6e6

Please sign in to comment.