public function testTemplateDefault1()
 {
     $doc = new XmlCompactDocument();
     $doc->load(self::samplesDir() . 'custom/runtime/templates/template_test_simple.xml');
     $test = $doc->getDocumentComponent();
     $sessionManager = new SessionManager($doc->getDocumentComponent());
     $storage = new TemporaryQtiBinaryStorage($sessionManager, new BinaryAssessmentTestSeeker($test));
     $session = $storage->instantiate($test);
     $sessionId = $session->getSessionId();
     // Let's try to persist a not begun session.
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // The session is instantiated, but not yet begun.
     $this->assertEquals(AssessmentTestSessionState::INITIAL, $session->getState());
     // The session begins...
     $session->beginTestSession();
     $this->assertEquals(AssessmentTestSessionState::INTERACTING, $session->getState());
     // We are in linear, non adaptive test. In this context, all item sessions
     // should be already begun.
     $QTPL1Sessions = $session->getAssessmentItemSessions('QTPL1');
     $QTPL1Session = $QTPL1Sessions[0];
     // The the session is correctly instantiated, with the <templateDefault>s in force.
     $this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL1Session->getState());
     $this->assertEquals(1.0, $QTPL1Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(0.0, $QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());
     $this->assertEquals(1.0, $QTPL1Session['GOODSCORE']->getValue());
     $this->assertEquals(0.0, $session['QTPL1.WRONGSCORE']->getValue());
     $this->assertEquals(0.0, $QTPL1Session['WRONGSCORE']->getValue());
     $QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
     $QTPL2Session = $QTPL2Sessions[0];
     $this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL2Session->getState());
     $this->assertEquals(2.0, $QTPL2Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(-1.0, $QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(2.0, $session['QTPL2.GOODSCORE']->getValue());
     $this->assertEquals(2.0, $QTPL2Session['GOODSCORE']->getValue());
     $this->assertEquals(-1.0, $session['QTPL2.WRONGSCORE']->getValue());
     $this->assertEquals(-1.0, $QTPL2Session['WRONGSCORE']->getValue());
     // Now let's make sure the persistence works correctly when <templateDefault>s are in force...
     // We do this by testing again that default values are correctly initialized within their respective
     // item sessions...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     $this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL1Session->getState());
     $this->assertEquals(1.0, $QTPL1Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(0.0, $QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());
     $this->assertEquals(1.0, $QTPL1Session['GOODSCORE']->getValue());
     $this->assertEquals(0.0, $session['QTPL1.WRONGSCORE']->getValue());
     $this->assertEquals(0.0, $QTPL1Session['WRONGSCORE']->getValue());
     $QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
     $QTPL2Session = $QTPL2Sessions[0];
     $this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL2Session->getState());
     $this->assertEquals(2.0, $QTPL2Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(-1.0, $QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
     $this->assertEquals(2.0, $session['QTPL2.GOODSCORE']->getValue());
     $this->assertEquals(2.0, $QTPL2Session['GOODSCORE']->getValue());
     $this->assertEquals(-1.0, $session['QTPL2.WRONGSCORE']->getValue());
     $this->assertEquals(-1.0, $QTPL2Session['WRONGSCORE']->getValue());
     // It seems to be ok! Let's take the test!
     $session->beginAttempt();
     // TPL1's responses should be applied their default values if any at the
     // beginning of the first attempt.
     $this->assertEquals(null, $session['QTPL1.RESPONSE']);
     // Noisy persistence ...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // TPL1's response should still be at their default.
     $this->assertEquals(null, $session['QTPL1.RESPONSE']);
     // -- TPL1 - Correct response.
     $candidateResponses = new State(array(new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::IDENTIFIER, new Identifier('ChoiceA'))));
     $session->endAttempt($candidateResponses);
     $this->assertEquals(1.0, $session['QTPL1.SCORE']->getValue());
     // Noisy persistence...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     $this->assertEquals('ChoiceA', $session['QTPL1.RESPONSE']->getValue());
     $this->assertEquals(1.0, $session['QTPL1.SCORE']->getValue());
     $session->moveNext();
     // Noisy persistence...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // -- TPL2 - Correct response.
     $session->beginAttempt();
     // TPL2's responses should be at their default values if any at
     // the beginning of the first attempt.
     $this->assertEquals(null, $session['QTPL2.RESPONSE']);
     // Noisy persistence ...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // TPL2's response should still be at their default.
     $this->assertEquals(null, $session['QTPL2.RESPONSE']);
     // -- TPL2 - Incorrect response.
     $candidateResponses = new State(array(new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::IDENTIFIER, new Identifier('ChoiceC'))));
     $session->endAttempt($candidateResponses);
     $this->assertEquals(-1.0, $session['QTPL2.SCORE']->getValue());
     // Noisy persistence...
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     $this->assertEquals('ChoiceC', $session['QTPL2.RESPONSE']->getValue());
     $this->assertEquals(-1.0, $session['QTPL2.SCORE']->getValue());
     // -- Go to the end of test.
     $session->moveNext();
     // Check states...
     $QTPL1Sessions = $session->getAssessmentItemSessions('QTPL1');
     $QTPL1Session = $QTPL1Sessions[0];
     $QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
     $QTPL2Session = $QTPL2Sessions[0];
     $this->assertEquals(AssessmentTestSessionState::CLOSED, $session->getState());
     $this->assertEquals(AssessmentItemSessionState::CLOSED, $QTPL1Session->getState());
     $this->assertEquals(AssessmentItemSessionState::CLOSED, $QTPL2Session->getState());
 }
 public function testFiles()
 {
     $doc = new XmlCompactDocument();
     $doc->load(self::samplesDir() . 'custom/runtime/files/files.xml');
     $test = $doc->getDocumentComponent();
     $sessionManager = new SessionManager($doc->getDocumentComponent());
     $storage = new TemporaryQtiBinaryStorage($sessionManager, new BinaryAssessmentTestSeeker($test));
     $session = $storage->instantiate($test);
     $session->beginTestSession();
     $sessionId = $session->getSessionId();
     // --- Q01 - files_1.txt = ('text.txt', 'text/plain', 'Some text...')
     $session->beginAttempt();
     $filepath = self::samplesDir() . 'datatypes/file/files_1.txt';
     $session->endAttempt(new State(array(new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::FILE, FileSystemFile::retrieveFile($filepath)))));
     $session->moveNext();
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q01.RESPONSE']);
     $this->assertEquals('text.txt', $session['Q01.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q01.RESPONSE']->getMimeType());
     $this->assertEquals('Some text...', $session['Q01.RESPONSE']->getData());
     // Let's persist and retrieve and look if we have the same value in Q01.RESPONSE.
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q01.RESPONSE']);
     $this->assertEquals('text.txt', $session['Q01.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q01.RESPONSE']->getMimeType());
     $this->assertEquals('Some text...', $session['Q01.RESPONSE']->getData());
     // --- Q02 - files_2.txt = ('', 'text/html', '<img src="/qtism/img.png"/>')
     $session->beginAttempt();
     $filepath = self::samplesDir() . 'datatypes/file/files_2.txt';
     $session->endAttempt(new State(array(new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::FILE, FileSystemFile::retrieveFile($filepath)))));
     $session->moveNext();
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q02.RESPONSE']);
     $this->assertEquals('', $session['Q02.RESPONSE']->getFilename());
     $this->assertEquals('text/html', $session['Q02.RESPONSE']->getMimeType());
     $this->assertEquals('<img src="/qtism/img.png"/>', $session['Q02.RESPONSE']->getData());
     // Again, we persist and retrieve.
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // We now test all the collected variables.
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q01.RESPONSE']);
     $this->assertEquals('text.txt', $session['Q01.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q01.RESPONSE']->getMimeType());
     $this->assertEquals('Some text...', $session['Q01.RESPONSE']->getData());
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q02.RESPONSE']);
     $this->assertEquals('', $session['Q02.RESPONSE']->getFilename());
     $this->assertEquals('text/html', $session['Q02.RESPONSE']->getMimeType());
     $this->assertEquals('<img src="/qtism/img.png"/>', $session['Q02.RESPONSE']->getData());
     // --- Q03 - files_3.txt ('empty.txt', 'text/plain', '')
     $session->beginAttempt();
     $filepath = self::samplesDir() . 'datatypes/file/files_3.txt';
     $session->endAttempt(new State(array(new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::FILE, FileSystemFile::retrieveFile($filepath)))));
     $session->moveNext();
     $this->assertFalse($session->isRunning());
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q02.RESPONSE']);
     $this->assertEquals('empty.txt', $session['Q03.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q03.RESPONSE']->getMimeType());
     $this->assertEquals('', $session['Q03.RESPONSE']->getData());
     $storage->persist($session);
     unset($session);
     $session = $storage->retrieve($test, $sessionId);
     // Final big check.
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q01.RESPONSE']);
     $this->assertEquals('text.txt', $session['Q01.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q01.RESPONSE']->getMimeType());
     $this->assertEquals('Some text...', $session['Q01.RESPONSE']->getData());
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q02.RESPONSE']);
     $this->assertEquals('', $session['Q02.RESPONSE']->getFilename());
     $this->assertEquals('text/html', $session['Q02.RESPONSE']->getMimeType());
     $this->assertEquals('<img src="/qtism/img.png"/>', $session['Q02.RESPONSE']->getData());
     $this->assertInstanceOf('qtism\\common\\datatypes\\File', $session['Q02.RESPONSE']);
     $this->assertEquals('empty.txt', $session['Q03.RESPONSE']->getFilename());
     $this->assertEquals('text/plain', $session['Q03.RESPONSE']->getMimeType());
     $this->assertEquals('', $session['Q03.RESPONSE']->getData());
 }