Source of file AnswerTest.php
Size: 6,881 Bytes - Last Modified: 2021-12-24T06:40:50+00:00
/var/www/docs.ssmods.com/process/src/tests/units/AnswerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | <?php namespace CodeCraft\Pathfinder\Tests\Units; use CodeCraft\Pathfinder\Model\Answer; use CodeCraft\Pathfinder\Model\Flow; use CodeCraft\Pathfinder\Model\Pathfinder; use CodeCraft\Pathfinder\Model\PathfinderPage; use CodeCraft\Pathfinder\Model\Question; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\ORM\FieldType\DBText; use SilverStripe\ORM\UnsavedRelationList; /** * Testing a pathfinder question's answer */ class AnswerTest extends SapphireTest { /** * @var string */ protected static $fixture_file = '../fixtures/fixture-pathfinder.yml'; /** * @return void */ public function testPopulateDefaults() { $answer = Answer::create(); $this->assertCount( 1, $answer->Choices(), 'Should populate a default choice' ); } /** * @return void */ public function testGetPathfinder() { $answer = Answer::create(); $this->assertInstanceOf( Pathfinder::class, $answer->getPathfinder(), 'Should scaffold a pathfinder' ); /** @var PathfinderPage $page */ $page = $this->objFromFixture(PathfinderPage::class, 'page'); $question = Question::create([ 'PathfinderID' => $page->Pathfinder()->ID, ]); $question->write(); $answer->QuestionID = $question->ID; $this->assertSame( $page->Pathfinder()->ID, $answer->getPathfinder()->ID, 'Should access related question\'s pathfinder' ); } /** * @return void */ public function testGetGoesToSummary() { /** @var PathfinderPage $page */ $page = $this->objFromFixture(PathfinderPage::class, 'page'); // Create this here instead of the fixture to take advantage of the auto-created pathfinder $nextQuestion = Question::create([ 'PathfinderID' => $page->Pathfinder()->ID, ]); $nextQuestion->write(); $answer = Answer::create(); $this->assertSame( Answer::singleton()->getGoToResultsTitle(), $answer->getGoesToSummary(), 'Should indicate the answer goes to the results title' ); $answer->NextQuestions()->add($nextQuestion); $this->assertSame( $nextQuestion->getCMSTitle(), $answer->getGoesToSummary(), 'Should indicate the answer goes to the next question' ); } /** * @return void */ public function testGetGoesToField() { /** @var PathfinderPage $page */ $page = $this->objFromFixture(PathfinderPage::class, 'page'); $pathfinder = $page->Pathfinder(); // Create these here instead of the fixture to take advantage of the auto-created pathfinder $expectedFlowTitle1 = 'Flow 1'; $flow = Flow::create([ 'Title' => $expectedFlowTitle1, 'PathfinderID' => $pathfinder->ID, ]); $flow->write(); $expectedText1 = 'Question 1'; $question1 = Question::create([ 'QuestionText' => $expectedText1, 'PathfinderID' => $pathfinder->ID, ]); $question1->write(); $expectedText2 = 'Question 2'; $question2 = Question::create([ 'QID' => 'Custom', 'QuestionText' => $expectedText2, 'PathfinderID' => $pathfinder->ID, 'FlowID' => $flow->ID, ]); $question2->write(); $answer = Answer::create([ 'QuestionID' => $question1->ID, ]); $field = $answer->getGoesToField(); $this->assertInstanceOf( DropdownField::class, $field, 'Should produce a field' ); $this->assertSame( $answer->getGoToResultsTitle(), $field->getEmptyString(), 'Field should have results title as empty string' ); $this->assertSame( [ 'Flow_Default' => 'Default flow', 3 => 'Question 1 (Q: 3, in Flow: Flow 1)', 'Flow_1' => 'Flow 1', 4 => 'Question 2 (Q: Custom, in Flow: Flow 1)', ], $field->getSource(), 'Field should have a range of options based on flows and questions' ); } /** * @return void */ public function testSaveNextQuestionID() { $question = Question::create(); $question->write(); $answer = Answer::create(); $expected = false; try { $answer->saveNextQuestionID(999); } catch (\Silverstripe\ORM\ValidationException $e) { $expected = true; $this->assertContains( 'Question not found with', $e->getMessage() ); } $this->assertTrue( $expected, 'Should have caught an exception' ); $answer->saveNextQuestionID($question->ID); $this->assertContains( $question->ID, $answer->NextQuestions()->column(), 'Should add related question to answer' ); } /** * @return void */ public function testSmoke() { $answer = Answer::create(); $this->assertInstanceOf( Question::class, $answer->Question(), 'Should be able to smoke test Question()' ); $this->assertInstanceOf( UnsavedRelationList::class, $answer->Choices(), 'Should be able to smoke test Choices()' ); $this->assertInstanceOf( UnsavedRelationList::class, $answer->NextQuestions(), 'Should be able to smoke test NextQuestions()' ); $this->assertInstanceOf( FieldList::class, $answer->getCMSFields(), 'Should be able to smoke test getCMSFields()' ); $this->assertNotEmpty( $answer->getTitle(), 'Should be able to smoke test getTitle()' ); $this->assertFalse( $answer->getNextQuestion(), 'Should be able to smoke test getNextQuestion()' ); $this->assertNotEmpty( $answer->getAnswerSummary(), 'Should be able to smoke test getAnswerSummary()' ); $this->assertNotEmpty( $answer->getGoToResultsTitle(), 'Should be able to smoke test getGoToResultsTitle()' ); $answer->write(); $answer->delete(); $this->assertFalse( $answer->isInDB(), 'Should be able to smoke test onAfterDelete()' ); } } |