diff --git a/tests/_support/FunctionalTester.php b/tests/_support/FunctionalTester.php index 100a126c..0459be93 100644 --- a/tests/_support/FunctionalTester.php +++ b/tests/_support/FunctionalTester.php @@ -1,5 +1,12 @@ 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 * @@ -60,4 +87,47 @@ public function logout() $I->dontSeeAuthentication(); } + /** + * Create user and return entity + * + * @param bool|false $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); + } + + 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' => '
' . $this->faker->realText(300) . '
', + '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); + } + + } diff --git a/tests/functional/AdminCest.php b/tests/functional/AdminCest.php new file mode 100644 index 00000000..fe762974 --- /dev/null +++ b/tests/functional/AdminCest.php @@ -0,0 +1,29 @@ +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); + } + +} + diff --git a/tests/functional/ContentCest.php b/tests/functional/ContentCest.php new file mode 100644 index 00000000..8b8ae7c3 --- /dev/null +++ b/tests/functional/ContentCest.php @@ -0,0 +1,321 @@ +contentRepo = new ContentRepository(new Content(), new Dispatcher()); + $this->userRepo = new UserRepository(new User(), new Dispatcher()); + } + + 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' + ] + ], + $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' => 'ZZZ' + ] + ] + ); + $nonStickyContent = $I->haveContent( + [ + 'type' => 'content', + 'isActive' => 1, + 'isSticky' => 0, + 'parentId' => $category->id, + 'translations' => [ + 'langCode' => 'en', + 'title' => 'AAA' + ] + ] + ); + $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' => 'ZZZ' + ] + ] + ); + $nonPromotedContent = $I->haveContent( + [ + 'type' => 'content', + 'isActive' => 1, + 'isPromoted' => 0, + 'parentId' => $category->id, + 'translations' => [ + 'langCode' => 'en', + 'title' => 'AAA' + ] + ] + ); + $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 contentsAreOrderedByWeight(FunctionalTester $I) + { + $category = $I->haveContent(['type' => 'category', 'isActive' => 1]); + $route = '/' . $category->route->translations[0]['langCode'] . '/' . $category->route->translations[0]['url']; + $counter = 1; + $contents = []; + + do { + $contents[$counter] = $I->haveContent( + [ + 'isActive' => 1, + 'weight' => $counter, + 'parentId' => $category->id + ] + ); + $counter++; + } while ($counter <= 10); + + $I->wantTo('check if heavier contents go to bottom'); + $I->amOnPage($route); + $I->seeResponseCodeIs(200); + + $counter = 1; + + do { + $I->see($contents[$counter]->translations[0]->title, "(//h2)[{$counter}]"); + $counter++; + } while ($counter <= 10); + } + + 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 canSeeArticleInNotPublishedCategoryAsAdmin(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']; + + $I->wantTo('see article in not published category as admin'); + $I->loginAsAdmin(); + $I->amOnPage($contentRoute); + $I->seeResponseCodeIs(200); + + $I->see($content->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 cantSeeArticleInNotPublishedCategoryAsRegularUser(FunctionalTester $I) + { + $category = $I->haveContent(['type' => 'category', 'isActive' => 0]); + $content = $I->haveContent(['type' => 'content', 'isActive' => 1, 'parentId' => $category->id]); + $contentRoute = '/' . $content->route->translations[0]['langCode'] . '/' . $content->route->translations[0]['url']; + + $I->wantTo('cant see article in not published category as regular user'); + $I->amOnPage($contentRoute); + $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'); + } + +}