95 lines
3.9 KiB
PHP
95 lines
3.9 KiB
PHP
<?php
|
||
|
||
namespace Tests\App\Libraries;
|
||
|
||
use App\Libraries\AssessmentDocxReader;
|
||
use CodeIgniter\Test\CIUnitTestCase;
|
||
use PhpOffice\PhpWord\PhpWord;
|
||
use PhpOffice\PhpWord\IOFactory;
|
||
|
||
final class AssessmentDocxReaderTest extends CIUnitTestCase
|
||
{
|
||
private const QUESTIONS = [
|
||
['id' => 11, 'text' => 'What is the student’s Islamic education background?'],
|
||
['id' => 12, 'text' => 'What is the student’s Arabic competency level?'],
|
||
['id' => 13, 'text' => 'Any other questions / concerns that the parents may have?'],
|
||
];
|
||
|
||
public function testMapsNumberedParagraphQuestionsAndMultilineAnswers(): void
|
||
{
|
||
$result = (new AssessmentDocxReader())->mapAnswers([
|
||
'Student intake form',
|
||
'1. What is the student\'s Islamic education background?',
|
||
'Two years at home.',
|
||
'Weekly mosque classes.',
|
||
'2) What is the student’s Arabic competency level? Beginner reader',
|
||
'3. Any other questions / concerns that the parents may have?',
|
||
'None.',
|
||
], self::QUESTIONS);
|
||
|
||
$this->assertSame("Two years at home.\nWeekly mosque classes.", $result['answers'][11]);
|
||
$this->assertSame('Beginner reader', $result['answers'][12]);
|
||
$this->assertSame('None.', $result['answers'][13]);
|
||
$this->assertSame([], $result['missing']);
|
||
}
|
||
|
||
public function testReadsParagraphsAndTableCellsFromDocx(): void
|
||
{
|
||
$phpWord = new PhpWord();
|
||
$section = $phpWord->addSection();
|
||
$section->addText(self::QUESTIONS[0]['text']);
|
||
$section->addText('Studied for one year.');
|
||
$table = $section->addTable();
|
||
$table->addRow();
|
||
$table->addCell()->addText(self::QUESTIONS[1]['text']);
|
||
$table->addCell()->addText('Conversational');
|
||
|
||
$path = tempnam(sys_get_temp_dir(), 'assessment-docx-');
|
||
$this->assertNotFalse($path);
|
||
$docxPath = $path . '.docx';
|
||
try {
|
||
IOFactory::createWriter($phpWord, 'Word2007')->save($docxPath);
|
||
$result = (new AssessmentDocxReader())->read($docxPath, array_slice(self::QUESTIONS, 0, 2));
|
||
} finally {
|
||
@unlink($path);
|
||
@unlink($docxPath);
|
||
}
|
||
|
||
$this->assertSame('Studied for one year.', $result['answers'][11]);
|
||
$this->assertSame('Conversational', $result['answers'][12]);
|
||
}
|
||
|
||
public function testReportsQuestionsWithoutAnswers(): void
|
||
{
|
||
$result = (new AssessmentDocxReader())->mapAnswers([
|
||
self::QUESTIONS[0]['text'],
|
||
'An answer.',
|
||
], self::QUESTIONS);
|
||
|
||
$this->assertSame([11 => 'An answer.'], $result['answers']);
|
||
$this->assertArrayHasKey(12, $result['missing']);
|
||
$this->assertArrayHasKey(13, $result['missing']);
|
||
}
|
||
|
||
public function testAllowsAnOmittedQuestionWordAndStopsAtAdministrativeNotes(): void
|
||
{
|
||
$questions = [
|
||
['id' => 21, 'text' => 'If surnames of enrolled students are different, please confirm if they are all siblings; if NOT, then please specify their relationship to each other:'],
|
||
self::QUESTIONS[2],
|
||
];
|
||
$result = (new AssessmentDocxReader())->mapAnswers([
|
||
'If surnames of enrolled students are different, please confirm if they are all siblings; if NOT, then please their relationship to each other:',
|
||
'N/A',
|
||
'Any other questions / concerns that the parents may have:',
|
||
'Question 1: Please focus on completing the curriculum.',
|
||
'Principal Notes:',
|
||
'This must not become part of the parent answer.',
|
||
], $questions);
|
||
|
||
$this->assertSame('N/A', $result['answers'][21]);
|
||
$this->assertSame('Question 1: Please focus on completing the curriculum.', $result['answers'][13]);
|
||
$this->assertStringNotContainsString('Principal Notes', $result['answers'][13]);
|
||
$this->assertSame([], $result['missing']);
|
||
}
|
||
}
|