Skip to content

Commit

Permalink
tags, bookmarks, ratings, section, frontend fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Redjik committed Nov 25, 2015
1 parent d3a1e9d commit d71e9a6
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 220 deletions.
8 changes: 4 additions & 4 deletions api/app/Http/Controllers/TagController.php
Expand Up @@ -12,7 +12,7 @@

use App\Http\Transformers\EloquentModelTransformer;
use App\Models\Tag;
use Illuminate\Http\Request;
use Spira\Responder\Response\ApiResponse;

class TagController extends EntityController
{
Expand All @@ -24,12 +24,12 @@ public function __construct(Tag $model, EloquentModelTransformer $transformer)
/**
* Get tags of a particular type.
*
* @param Request $request
* @param string $id
* @param string $group
* @return ApiResponse
*/
public function getGroupTags(Request $request, $group)
public function getGroupTags($group)
{
/** @var Tag $tagGroupTag */
$tagGroupTag = Tag::where('tag', '=', urldecode($group))->firstOrFail();

$groupTags = $tagGroupTag->childTags()->get();
Expand Down
13 changes: 7 additions & 6 deletions api/app/Models/AbstractPost.php
Expand Up @@ -10,6 +10,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Rhumsaa\Uuid\Uuid;
use Illuminate\Support\Str;
use App\Models\Traits\TagTrait;
Expand Down Expand Up @@ -41,14 +42,14 @@
*
* relations
*
* @property PostPermalink[] $permalinks
* @property Meta[] $metas
* @property PostPermalink[]|Collection $permalinks
* @property Meta[]|Collection $metas
* @property Image $thumbnailImage
* @property User $author
* @property Section[] $sections
* @property Tag[] $tags
* @property Rating[] $userRatings
* @property Bookmark[] $bookmarks
* @property Section[]|Collection $sections
* @property Tag[]|Collection $tags
* @property Rating[]|Collection $userRatings
* @property Bookmark[]|Collection $bookmarks
*/
class AbstractPost extends IndexedModel implements LocalizableModelInterface
{
Expand Down
58 changes: 4 additions & 54 deletions api/app/Models/Tag.php
Expand Up @@ -32,6 +32,8 @@ class Tag extends IndexedModel
*/
protected $fillable = ['tag_id','tag', 'searchable'];

protected $touches = ['articles'];

protected static $validationRules = [
'tag_id' => 'required|uuid',
'tag' => 'required|string|max:30',
Expand All @@ -49,24 +51,6 @@ class Tag extends IndexedModel
],
];

protected $taggedModels = [
'articles' => Article::class,
];

protected static function boot()
{
//auto touching
static::booted(function (Tag $model) {
$touches = array_merge($model->touches, array_keys($model->taggedModels));
$touches = array_unique($touches);
$model->setTouchedRelations($touches);

return true;
});

parent::boot();
}

/**
* @param mixed $id
* @return BaseModel
Expand Down Expand Up @@ -140,42 +124,8 @@ public function parentTags()
]);
}

/**
* Get a relationship.
*
* @param string $key
* @return mixed
*/
public function getRelationValue($key)
public function articles()
{
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if ($this->relationLoaded($key)) {
return $this->relations[$key];
}

// If the "attribute" exists as a method on the model, we will just assume
// it is a relationship and will load and return results from the query
// and hydrate the relationship's value on the "relationships" array.
if (isset($this->taggedModels[$key]) || method_exists($this, $key)) {
return $this->getRelationshipFromMethod($key);
}
}

/**
* Handle dynamic method calls into the model.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (isset($this->taggedModels[$method])) {
return $this->belongsToMany($this->taggedModels[$method], null, null, null, $method)->withPivot('tag_group_id', 'tag_group_parent_id');
}

return parent::__call($method, $parameters);
return $this->belongsToMany(Article::class, 'post_tag', 'tag_id', 'post_id', 'articles')->withPivot('tag_group_id', 'tag_group_parent_id');
}
}
93 changes: 50 additions & 43 deletions api/tests/integration/ArticleBookmarkRateTest.php
Expand Up @@ -8,160 +8,167 @@
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

use App\Models\AbstractPost;
use App\Models\Article;

class ArticleBookmarkRateTest extends TestCase
{
protected $baseRoute = '/articles';
protected $factoryClass = Article::class;

protected $ratedName = 'ratedArticles';
protected $bookmarkedName = 'bookmarkedArticles';

public function testSimpleRate()
{
$user = $this->createUser();
$article = $this->getFactory(Article::class)->create();
$post = $this->getFactory($this->factoryClass)->create();
$rateData = $this->getFactory(App\Models\Rating::class)->transformed();

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->putJson('articles/'.$article->article_id.'/ratings/'.$rateData['ratingId'], $rateData);
$this->withAuthorization('Bearer '.$token)->putJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rateData['ratingId'], $rateData);

$this->assertResponseStatus(201);

$this->assertEquals($user->ratedArticles->first()->article_id, $article->article_id);
$rating = $user->ratedArticles->first()->pivot->rating_value;
$this->assertEquals($user->{$this->ratedName}->first()->post_id, $post->post_id);
$rating = $user->{$this->ratedName}->first()->pivot->rating_value;
$this->assertTrue($rating > 0 && $rating < 6);
}

public function testInvalidRate()
{
$user = $this->createUser();
$article = $this->getFactory(Article::class)->create();
$post = $this->getFactory($this->factoryClass)->create();
$rateData = $this->getFactory(App\Models\Rating::class)->customize(['rating_value' => 6])->transformed();

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->putJson('articles/'.$article->article_id.'/ratings/'.$rateData['ratingId'], $rateData);
$this->withAuthorization('Bearer '.$token)->putJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rateData['ratingId'], $rateData);

$this->assertException('There was an issue with the validation of provided entity', 422, 'ValidationException');
}

public function testSimpleBookmark()
{
$user = $this->createUser();
$article = $this->getFactory(Article::class)->create();
$post = $this->getFactory($this->factoryClass)->create();
$bookmarkData = $this->getFactory(App\Models\Bookmark::class)->transformed();

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->putJson('articles/'.$article->article_id.'/bookmarks/'.$bookmarkData['bookmarkId'], $bookmarkData);
$this->withAuthorization('Bearer '.$token)->putJson($this->baseRoute.'/'.$post->post_id.'/bookmarks/'.$bookmarkData['bookmarkId'], $bookmarkData);

$this->assertResponseStatus(201);

$this->assertEquals($user->bookmarkedArticles->first()->article_id, $article->article_id);
$this->assertEquals($user->{$this->bookmarkedName}->first()->post_id, $post->post_id);
}

public function testRemoveRate()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$rating = $this->getFactory(App\Models\Rating::class)->make(['user_id' => $user->user_id]);
$article->userRatings()->save($rating);
$post->userRatings()->save($rating);

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->deleteJson('articles/'.$article->article_id.'/ratings/'.$rating->rating_id);
$this->withAuthorization('Bearer '.$token)->deleteJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rating->rating_id);

$this->assertResponseStatus(204);
$this->assertEquals(0, $user->ratedArticles->count());
$this->assertEquals(0, $user->{$this->ratedName}->count());
}

public function testRemoveBookmark()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$bookmark = $this->getFactory(App\Models\Bookmark::class)->make(['user_id' => $user->user_id]);
$article->bookmarks()->save($bookmark);
$post->bookmarks()->save($bookmark);

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->deleteJson('articles/'.$article->article_id.'/bookmarks/'.$bookmark->bookmark_id);
$this->withAuthorization('Bearer '.$token)->deleteJson($this->baseRoute.'/'.$post->post_id.'/bookmarks/'.$bookmark->bookmark_id);

$this->assertResponseStatus(204);
$this->assertEquals(0, $user->bookmarkedArticles->count());
$this->assertEquals(0, $user->{$this->bookmarkedName}->count());
}

public function testUpdateRate()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$rating = $this->getFactory(App\Models\Rating::class)->make(
[
'user_id' => $user->user_id,
'rating_value' => 5,
]);

$article->userRatings()->save($rating);
$post->userRatings()->save($rating);

$this->assertEquals(5, $article->userRatings->first()->rating_value);
$this->assertEquals(1, $article->userRatings->count());
$this->assertEquals(5, $post->userRatings->first()->rating_value);
$this->assertEquals(1, $post->userRatings->count());

$token = $this->tokenFromUser($user);
$this->withAuthorization('Bearer '.$token)->putJson('articles/'.$article->article_id.'/ratings/'.$rating->rating_id, [
$this->withAuthorization('Bearer '.$token)->putJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rating->rating_id, [
'ratingId' => $rating->rating_id,
'ratingValue' => 2,
]);

$article = Article::find($article->article_id);
$class = $this->factoryClass;
$post = $class::find($post->post_id);

$this->assertResponseStatus(201);
$this->assertEquals(1, $article->userRatings->count());
$this->assertEquals(2, $article->userRatings->first()->rating_value);
$this->assertEquals(1, $post->userRatings->count());
$this->assertEquals(2, $post->userRatings->first()->rating_value);
}

public function testRemoveRateSpoof()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$rating = $this->getFactory(App\Models\Rating::class)->make(['user_id' => $user->user_id]);
$article->userRatings()->save($rating);
$post->userRatings()->save($rating);

$spoofer = $this->createUser();
$token = $this->tokenFromUser($spoofer);
$this->withAuthorization('Bearer '.$token)->deleteJson('articles/'.$article->article_id.'/ratings/'.$rating->rating_id);
$this->withAuthorization('Bearer '.$token)->deleteJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rating->rating_id);

$this->assertException('Denied', 403, 'ForbiddenException');
}

public function testRemoveBookmarkSpoof()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$bookmark = $this->getFactory(App\Models\Bookmark::class)->make(['user_id' => $user->user_id]);
$article->bookmarks()->save($bookmark);
$post->bookmarks()->save($bookmark);

$spoofer = $this->createUser();
$token = $this->tokenFromUser($spoofer);
$this->withAuthorization('Bearer '.$token)->deleteJson('articles/'.$article->article_id.'/bookmarks/'.$bookmark->bookmark_id);
$this->withAuthorization('Bearer '.$token)->deleteJson($this->baseRoute.'/'.$post->post_id.'/bookmarks/'.$bookmark->bookmark_id);

$this->assertException('Denied', 403, 'ForbiddenException');
}

public function testUpdateRateSpoof()
{
$user = $this->createUser();
/** @var Article $article */
$article = $this->getFactory(Article::class)->create();
/** @var AbstractPost $post */
$post = $this->getFactory($this->factoryClass)->create();
$rating = $this->getFactory(App\Models\Rating::class)->make(
[
'user_id' => $user->user_id,
'rating_value' => 5,
]);

$article->userRatings()->save($rating);
$post->userRatings()->save($rating);

$this->assertEquals(5, $article->userRatings->first()->rating_value);
$this->assertEquals(1, $article->userRatings->count());
$this->assertEquals(5, $post->userRatings->first()->rating_value);
$this->assertEquals(1, $post->userRatings->count());

$spoofer = $this->createUser();
$token = $this->tokenFromUser($spoofer);
$this->withAuthorization('Bearer '.$token)->putJson('articles/'.$article->article_id.'/ratings/'.$rating->rating_id, [
$this->withAuthorization('Bearer '.$token)->putJson($this->baseRoute.'/'.$post->post_id.'/ratings/'.$rating->rating_id, [
'ratingId' => $rating->rating_id,
'ratingValue' => 2,
]);
Expand Down

0 comments on commit d71e9a6

Please sign in to comment.