Skip to content

Commit

Permalink
Test to create Answers and voting system
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Sep 8, 2023
1 parent 42e6661 commit 9d821df
Show file tree
Hide file tree
Showing 22 changed files with 370 additions and 276 deletions.
46 changes: 23 additions & 23 deletions tests/codeception.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
actor: Tester
namespace: questions
bootstrap: _bootstrap.php
settings:
suite_class: \PHPUnit_Framework_TestSuite
colors: true
shuffle: false
memory_limit: 1024M
log: true

# This value controls whether PHPUnit attempts to backup global variables
# See https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.backupGlobals
backup_globals: true
paths:
tests: codeception
log: codeception/_output
data: codeception/_data
helpers: codeception/_support
envs: ../../../humhub/tests/config/env
config:
# the entry script URL (with host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: http://localhost:8080/index-test.php
actor: Tester
namespace: questions
bootstrap: _bootstrap.php
settings:
suite_class: \PHPUnit_Framework_TestSuite
colors: true
shuffle: false
memory_limit: 1024M
log: true

# This value controls whether PHPUnit attempts to backup global variables
# See https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.backupGlobals
backup_globals: true
paths:
tests: codeception
log: codeception/_output
data: codeception/_data
helpers: codeception/_support
envs: ../../../humhub/tests/config/env
config:
# the entry script URL (with host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: http://localhost:8080/index-test.php
70 changes: 35 additions & 35 deletions tests/codeception/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

/*
* This is the initial test bootstrap, which will load the default test bootstrap from the humhub core
*/

$testRoot = dirname(__DIR__);

\Codeception\Configuration::append(['test_root' => $testRoot]);
codecept_debug('Module root: ' . $testRoot);

$humhubPath = getenv('HUMHUB_PATH');
if ($humhubPath === false) {
// If no environment path was set, we assume residing in default the modules directory
$moduleConfig = require $testRoot . '/config/test.php';
if (isset($moduleConfig['humhub_root'])) {
$humhubPath = $moduleConfig['humhub_root'];
} else {
$humhubPath = dirname(__DIR__, 5);
}
}

\Codeception\Configuration::append(['humhub_root' => $humhubPath]);
codecept_debug('HumHub Root: ' . $humhubPath);

// Load test configuration (/config/test.php or /config/env/<environment>/test.php
$globalConfig = require $humhubPath . '/protected/humhub/tests/codeception/_loadConfig.php';

// Load default test bootstrap (initialize Yii...)
require $globalConfig['humhub_root'] . '/protected/humhub/tests/codeception/_bootstrap.php';
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

/*
* This is the initial test bootstrap, which will load the default test bootstrap from the humhub core
*/

$testRoot = dirname(__DIR__);

\Codeception\Configuration::append(['test_root' => $testRoot]);
codecept_debug('Module root: ' . $testRoot);

$humhubPath = getenv('HUMHUB_PATH');
if ($humhubPath === false) {
// If no environment path was set, we assume residing in default the modules directory
$moduleConfig = require $testRoot . '/config/test.php';
if (isset($moduleConfig['humhub_root'])) {
$humhubPath = $moduleConfig['humhub_root'];
} else {
$humhubPath = dirname(__DIR__, 5);
}
}

\Codeception\Configuration::append(['humhub_root' => $humhubPath]);
codecept_debug('HumHub Root: ' . $humhubPath);

// Load test configuration (/config/test.php or /config/env/<environment>/test.php
$globalConfig = require $humhubPath . '/protected/humhub/tests/codeception/_loadConfig.php';

// Load default test bootstrap (initialize Yii...)
require $globalConfig['humhub_root'] . '/protected/humhub/tests/codeception/_bootstrap.php';
6 changes: 3 additions & 3 deletions tests/codeception/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Ignore everything in this directory
*
# Except this file
# Ignore everything in this directory
*
# Except this file
!.gitignore
86 changes: 83 additions & 3 deletions tests/codeception/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace questions;

use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\questions\models\Question;

/**
* Inherited Methods
* @method void wantToTest($text)
Expand All @@ -26,7 +29,84 @@ class AcceptanceTester extends \AcceptanceTester
{
use _generated\AcceptanceTesterActions;

/**
* Define custom actions here
*/
public function createQuestion(string $headline, string $description)
{
$this->amGoingTo('create a Question');
$this->waitForText('Q&A');
$this->click( 'Q&A', '#contentFormMenu');
$this->waitForElementVisible('input[name="Question[question]"]');
$this->fillField('Question[question]', $headline);
$this->fillField('#question-description .humhub-ui-richtext', $description);
$this->jsClick('#post_submit_button');

$this->waitForText($headline, null, '.wall-entry-header');
$this->see('Provide an answer', '[data-content-component="questions.Question"]');
$this->see('View all answers', '[data-content-component="questions.Question"]');
}

public function provideAnswer(string $answerText)
{
$this->waitForText('Provide an answer', null, '.field-questionanswer-answer');
$this->fillField('#answerRichTextField0 .humhub-ui-richtext', $answerText);
$this->click('Save', '.questions-answer-form');
$this->waitForText($answerText, null, '.except-best-answers');
}

private function getVoteButtonSelector(int $answerId, string $suffix = ''): string
{
return '[data-answer="' . $answerId . '"] [data-action-click="vote"]' . $suffix;
}

public function canVote(int $answerId)
{
$this->seeElement($this->getVoteButtonSelector($answerId, ':not([disabled])'));
}

public function cannotVote(int $answerId)
{
$this->seeElement($this->getVoteButtonSelector($answerId, '[disabled]'));
}

public function vote(int $answerId, string $buttonTitle)
{
$this->canVote($answerId);
$buttonSelector = $this->getVoteButtonSelector($answerId, '[data-original-title="' . $buttonTitle . '"]');
$this->jsClick($buttonSelector);
$this->waitForElementVisible($buttonSelector . '.active');
}

public function upVote(int $answerId)
{
$this->vote($answerId, 'Upvote');
}

public function downVote(int $answerId)
{
$this->vote($answerId, 'Downvote');
}

public function checkVotingSummary(int $answerId, int $summary)
{
$this->see($summary, '[data-answer="' . $answerId . '"] .questions-answer-voting div');
}

public function selectBestAnswer(int $answerId)
{
$answerSelector = '[data-answer="' . $answerId . '"]';
$this->moveMouseOver(['css' => $answerSelector]);
$bestButtonSelector = '[data-action-click="best"]';
$this->waitForElementVisible($bestButtonSelector);
$this->jsClick($answerSelector . ' ' . $bestButtonSelector);
$this->waitForElementVisible($answerSelector . '.questions-best-answer');
}

public function unselectBestAnswer()
{
$this->waitForElementVisible('.questions-best-answer');
$bestButtonSelector = '[data-action-click="best"]';
$this->waitForElementVisible($bestButtonSelector);
$this->jsClick('.questions-best-answer ' . $bestButtonSelector);
$this->wait(1);
$this->dontSeeElement('.questions-best-answer');
}
}
6 changes: 3 additions & 3 deletions tests/codeception/_support/_generated/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Ignore everything in this directory
*
# Except this file
# Ignore everything in this directory
*
# Except this file
!.gitignore
48 changes: 24 additions & 24 deletions tests/codeception/acceptance.suite.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: AcceptanceTester
modules:
enabled:
- WebDriver
- tests\codeception\_support\WebHelper
- tests\codeception\_support\DynamicFixtureHelper
config:
WebDriver:
url: 'http://localhost:8080/'
browser: chrome
restart: true
port: 4444
capabilities:
chromeOptions:
# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: AcceptanceTester
modules:
enabled:
- WebDriver
- tests\codeception\_support\WebHelper
- tests\codeception\_support\DynamicFixtureHelper
config:
WebDriver:
url: 'http://localhost:8080/'
browser: chrome
restart: true
port: 4444
capabilities:
chromeOptions:
args: ["--lang=en-US"]
114 changes: 69 additions & 45 deletions tests/codeception/acceptance/QuestionCest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace questions\acceptance;

use questions\AcceptanceTester;

class QuestionCest
{

public function testCreateQuestion(AcceptanceTester $I)
{
$I->wantTo('create a Question from stream');
$I->amAdmin();

$I->amGoingTo('install the questions module for space 1');
$I->enableModule(1, 'questions');
$I->amOnSpace1();

$I->amGoingTo('create a Question');
$I->waitForText('Q&A');
$I->click( 'Q&A', '#contentFormMenu');
$I->waitForElementVisible('input[name="Question[question]"]');
$I->fillField('Question[question]', 'Question headline text?');
$I->fillField('#question-description .humhub-ui-richtext', 'Question description text.');
$I->jsClick('#post_submit_button');

$I->waitForText('Question headline text?', null, '.wall-entry-header');
$I->see('Provide an answer', '[data-content-component="questions.Question"]');
$I->see('View all answers (0)', '[data-content-component="questions.Question"]');

$I->amGoingTo('provide an Answer');
$I->click('Provide an answer');
$I->switchToNextTab();
$I->waitForText('Collapse all answers (0)');
$I->fillField('#answerRichTextField0 .humhub-ui-richtext', 'Answer text.');
$I->click('Save', '.questions-answer-form');
$I->waitForText('Answer text.', null, '.except-best-answers');
}

}
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace questions\acceptance;

use questions\AcceptanceTester;

class QuestionCest
{

public function testCreateQuestionWithAnswersAndTestVoting(AcceptanceTester $I)
{
$I->wantTo('create a Question from stream');
$I->amAdmin();

$I->amGoingTo('install the questions module for space 1');
$I->enableModule(1, 'questions');
$I->amOnSpace1();

$I->createQuestion('Question headline text?', 'Question description text.');

$I->amGoingTo('provide first Answer from Admin');
$I->click('Provide an answer');
$I->switchToNextTab();
$I->provideAnswer('First answer text from Admin.');
$I->cannotVote(1);

$I->amUser2(true);
$I->amOnSpace1();

$I->amGoingTo('provide first Answer from Sara');
$I->waitForText('Provide an answer', null, '.wall-entry');
$I->click('Provide an answer');
$I->switchToNextTab();
$I->provideAnswer('Second answer text from Sara.');
$I->cannotVote(2);

$I->amGoingTo('vote on the first Answer of Admin');
$I->upVote(1);
$I->checkVotingSummary(1, 1);
$I->downVote(1);
$I->checkVotingSummary(1, -1);

$I->amAdmin(true);
$I->amOnSpace1();

$I->waitForText('View all answers (2)', null, '.wall-entry');
$I->click('View all answers (2)');
$I->switchToNextTab();

$I->amGoingTo('vote on the second Answer by Admin');
$I->upVote(2);
$I->checkVotingSummary(2, 1);
$I->wait(1);
$I->upVote(2);
$I->checkVotingSummary(2, 0);

$I->amGoingTo('select the best Answer');
$I->selectBestAnswer(2);
$I->unselectBestAnswer();
$I->selectBestAnswer(1);
$I->selectBestAnswer(2);
}

}
Loading

0 comments on commit 9d821df

Please sign in to comment.