Skip to content

Commit

Permalink
Merge cd2b04a into 093ced0
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszUrbanowicz committed Dec 2, 2015
2 parents 093ced0 + cd2b04a commit dfea975
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/_support/FunctionalTester.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php namespace platform;

use Faker\Factory;
use Gzero\Entity\Content;
use Gzero\Repository\ContentRepository;
use Gzero\Repository\UserRepository;
use Gzero\Entity\User;
use Illuminate\Events\Dispatcher;

/**
* Inherited Methods
* @method void wantToTest($text)
Expand All @@ -19,6 +26,29 @@ class FunctionalTester extends \Codeception\Actor {

use _generated\FunctionalTesterActions;

/**
* @var UserRepository
*/
private $userRepo;

/**
* @var ContentRepository
*/
private $contentRepo;

/**
* @var \Faker\Generator
*/
private $faker;

public function __construct(\Codeception\Scenario $scenario)
{
$this->faker = Factory::create();
$this->categoryRepo = new ContentRepository(new Content(), new Dispatcher());
$this->userRepo = new UserRepository(new User(), new Dispatcher());
parent::__construct($scenario);
}

/**
* Login in to page
*
Expand Down Expand Up @@ -60,4 +90,55 @@ public function logout()
$I->dontSeeAuthentication();
}

/**
* Create user and return entity
*
* @param array $attributes
*
* @return User
*/
public function haveUser($attributes = [])
{
$fakeAttributes = [
'firstName' => $this->faker->firstName,
'lastName' => $this->faker->lastName,
'email' => $this->faker->email
];

$fakeAttributes = array_merge($fakeAttributes, $attributes);

return $this->userRepo->create($fakeAttributes);
}

/**
* Create content and return entity
*
* @param bool|false $attributes
* @param null $user
*
* @return Content
*/
public function haveContent($attributes = false, $user = null)
{
$fakeAttributes = [
'type' => ['category', 'content'][rand(0, 1)],
'isActive' => 1,
'publishedAt' => date('Y-m-d H:i:s'),
'translations' => [
'langCode' => 'en',
'title' => $this->faker->realText(38, 1),
'teaser' => '<p>' . $this->faker->realText(300) . '</p>',
'body' => $this->faker->realText(1000),
'seoTitle' => $this->faker->realText(60, 1),
'seoDescription' => $this->faker->realText(160, 1),
'isActive' => rand(0, 1)
]
];

$fakeAttributes = array_merge($fakeAttributes, $attributes);

return $this->categoryRepo->create($fakeAttributes, $user);
}


}
29 changes: 29 additions & 0 deletions tests/functional/AdminCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace platform;

class AdminCest {

public function _before(FunctionalTester $I)
{
}

public function _after(FunctionalTester $I)
{
}

// tests
public function cantAccessAdminPanelAsRegularUser(FunctionalTester $I)
{
$I->wantTo('access admin panel as regular user');
$I->amOnPage('/admin');
$I->seeResponseCodeIs(404);
}

public function canAccessAdminPanelAsAdmin(FunctionalTester $I){
$I->wantTo('access admin panel as admin');
$I->loginAsAdmin();
$I->amOnPage('/admin');
$I->seeResponseCodeIs(200);
}

}

244 changes: 244 additions & 0 deletions tests/functional/ContentCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php namespace platform;

class ContentCest {

public function _before(FunctionalTester $I)
{
}

public function _after(FunctionalTester $I)
{
}

// tests
public function canViewArticle(FunctionalTester $I)
{
$user = $I->haveUser();
$content = $I->haveContent(['type' => 'content', 'isActive' => 1], $user);
$route = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url'];
$translation = $content->translations[0];

$I->wantTo('view article ' . $content->id);
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->canSee($translation->title);
$I->canSee($translation->body);
$I->canSee($user->firstName . ' ' . $user->lastName);
$I->canSee(date('d-m-Y', strtotime($content->createdAt)));
}


public function canUseArticleBreadcrumbs(FunctionalTester $I)
{
$user = $I->haveUser();
$category = $I->haveContent(
[
'type' => 'category',
'isActive' => 1,
'translations' => [
'title' => 'lorem ipsum',
'langCode' => 'en',
'isActive' => 1
]
],
$user
);
$content = $I->haveContent(['type' => 'content', 'isActive' => 1, 'parentId' => $category->id], $user);

$contentRoute = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url'];
$categoryRoute = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];
$linkName = ucwords($category->translations[0]->title);

$I->wantTo('use breadcrumbs to go back to category view from article');
$I->amOnPage($contentRoute);
$I->seeResponseCodeIs(200);

$I->seeLink($linkName, $categoryRoute);
$I->seeLink('Start', '/en');
$I->click($linkName);
$I->canSeeCurrentUrlEquals($categoryRoute);
}


public function canViewCategory(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$content = $I->haveContent(['type' => 'content', 'isActive' => 1, 'parentId' => $category->id]);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('view category');
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->see($category->translations[0]->title);
$I->see($content->translations[0]->title);
}


public function canGoToArticleFromCategory(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$content = $I->haveContent(['type' => 'content', 'isActive' => 1, 'parentId' => $category->id]);
$contentRoute = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url'];
$categoryRoute = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('read more');
$I->amOnPage($categoryRoute);
$I->seeResponseCodeIs(200);

$I->click('Read more');
$I->canSeeCurrentUrlEquals($contentRoute);
}

public function canSeeNotPublishedContentAsAdmin(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$content = $I->haveContent(['type' => 'content', 'isActive' => 0, 'parentId' => $category->id]);
$contentRoute = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url'];

$I->wantTo('see not published content as admin user');
$I->loginAsAdmin();
$I->amOnPage($contentRoute);
$I->seeResponseCodeIs(200);

$I->see($content->translations[0]->title);
$I->see('This content is not published.');
}

public function cantSeeNotPublishedContentAsRegularUser(FunctionalTester $I)
{
$content = $I->haveContent(['type' => 'content', 'isActive' => 0]);
$route = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url'];

$I->wantTo('try to see not published content as regular user');
$I->amOnPage($route);
$I->seeResponseCodeIs(404);
}

public function seeStickyContentOnTopOfTheList(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$stickyContent = $I->haveContent(
[
'type' => 'content',
'isActive' => 1,
'isSticky' => 1,
'parentId' => $category->id,
'translations' => [
'langCode' => 'en',
'title' => 'This content is sticky.',
'isActive' => 1
]
]
);
$nonStickyContent = $I->haveContent(
[
'type' => 'content',
'isActive' => 1,
'isSticky' => 0,
'parentId' => $category->id,
'translations' => [
'langCode' => 'en',
'title' => 'And this is not.',
'isActive' => 1
]
]
);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('see sticky content on the top of the list');
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->see($stickyContent->translations[0]->title, '(//h2)[1]');
}


public function seePromotedContentOnTopOfTheList(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$promotedContent = $I->haveContent(
[
'type' => 'content',
'isActive' => 1,
'isPromoted' => 1,
'parentId' => $category->id,
'translations' => [
'langCode' => 'en',
'title' => 'This content is promoted.',
'isActive' => 1
]
]
);
$nonPromotedContent = $I->haveContent(
[
'type' => 'content',
'isActive' => 1,
'isPromoted' => 0,
'parentId' => $category->id,
'translations' => [
'langCode' => 'en',
'title' => 'And this is not',
'isActive' => 1
]
]
);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('see sticky content on the top of the list');
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->see($promotedContent->translations[0]->title, '(//h2)[1]');
}

public function canSeeNotPublishedCategoryAsAdmin(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 0]);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('see not published category as admin');
$I->loginAsAdmin();
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->see($category->translations[0]->title);
$I->see('This content is not published.');
}

public function cantSeeNotPublishedCategoryAsUser(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 0]);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];

$I->wantTo('cant see unpublished category as user');
$I->amOnPage($route);
$I->seeResponseCodeIs(404);
}

public function canUsePagination(FunctionalTester $I)
{
$category = $I->haveContent(['type' => 'category', 'isActive' => 1]);
$route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url'];
$counter = 0;

do {
$I->haveContent(
[
'isActive' => 1,
'parentId' => $category->id
]
);
$counter++;
} while ($counter <= 40);

$I->wantTo('use pagination on category view');
$I->amOnPage($route);
$I->seeResponseCodeIs(200);

$I->click('2');
$I->canSeeCurrentUrlEquals($route . '?page=2');
}

}

0 comments on commit dfea975

Please sign in to comment.