Advertisement
sarjona

question/type/essay/tests/question_test.php

Feb 3rd, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1.     /**
  2.      * Test the behaviour of summarise_response().
  3.      *
  4.      * @dataProvider  summarise_response_text_attachments_provider
  5.      * @param integer $responserequired 0 or 1 depending on if the response is required or not.
  6.      * @param integer $attachmentsrequired Number or attachments required; -1 is unlimited.
  7.      * @param integer $filesattached Number of files attached.
  8.      * @return void
  9.      */
  10.     public function test_summarise_response_text_attachments(int $responserequired, int $attachmentsrequired, int $filesattached): void {
  11.         $this->resetAfterTest();
  12.  
  13.         // Create sample attachments.
  14.         $attachments = $this->create_user_and_sample_attachments($filesattached);
  15.  
  16.         // Create the essay question under test.
  17.         $essay = test_question_maker::make_an_essay_question();
  18.         $essay->start_attempt(new question_attempt_step(), 1);
  19.  
  20.         $essay->responserequired = $responserequired;
  21.         $essay->attachmentsrequired = $attachmentsrequired;
  22.         $essay->responseformat = 'editor';
  23.  
  24.         $textanswer = '';
  25.         $expected = '';
  26.         if ($responserequired) {
  27.             $textanswer = 'This is the text input for my essay.';
  28.             $expected .= $textanswer;
  29.         }
  30.         if ($filesattached) {
  31.             $expected .= 'Attachments:';
  32.             for ($i = 0; $i < $filesattached; $i++) {
  33.                 if ($i > 0) {
  34.                     $expected .= ',';
  35.                 }
  36.                 $expected .= " $i (1 bytes)";
  37.                 $result = $essay->summarise_response(
  38.                     ['answer' => $textanswer, 'answerformat' => FORMAT_HTML, 'attachments' => $attachments[$i + 1]]
  39.                 );
  40.                 $this->assertEquals($expected, $result);
  41.             }
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Data provider for test_summarise_response_text_attachments().
  47.      *
  48.      * @return array
  49.      */
  50.     public function summarise_response_text_attachments_provider(): array {
  51.         return [
  52.             'Text required, 1 attachments required' => [
  53.                 'responserequired' => 1,
  54.                 'attachmentsrequired' => 1,
  55.                 'filesattached' => 4,
  56.             ],
  57.             'Text not required, 4 attachments required' => [
  58.                 'responserequired' => 0,
  59.                 'attachmentsrequired' => 4,
  60.                 'filesattached' => 4,
  61.             ],
  62.             'Text not required, 4 attachments required' => [
  63.                 'responserequired' => 0,
  64.                 'attachmentsrequired' => -1,
  65.                 'filesattached' => 2,
  66.             ],
  67.         ];
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement