1717namespace qtype_aitext ;
1818
1919use coding_exception ;
20+ use core_reportbuilder \external \filters \set ;
2021use PHPUnit \Framework \ExpectationFailedException ;
2122use question_attempt_step ;
2223use SebastianBergmann \RecursionContext \InvalidArgumentException ;
@@ -42,8 +43,9 @@ final class question_test extends \advanced_testcase {
4243
4344 /**
4445 * Instance of the question type class
45- * @var qtype_aitext
46+ * @var question
4647 */
48+ public $ question ;
4749
4850 /**
4951 * There is a live connection to the External AI system
@@ -61,7 +63,7 @@ final class question_test extends \advanced_testcase {
6163 * @return void
6264 */
6365 protected function setUp (): void {
64- $ this ->qtype = new \qtype_aitext ();
66+ $ this ->question = new \qtype_aitext ();
6567 if (defined ('TEST_LLM_APIKEY ' ) && defined ('TEST_LLM_ORGID ' )) {
6668 set_config ('apikey ' , TEST_LLM_APIKEY , 'aiprovider_openai ' );
6769 set_config ('orgid ' , TEST_LLM_ORGID , 'aiprovider_openai ' );
@@ -103,6 +105,32 @@ public function test_get_question_summary(): void {
103105 $ this ->assertEquals ('Hello [world] ' , $ aitext ->get_question_summary ());
104106 }
105107
108+ /**
109+ * Check on some permutations of how the prompt that is sent to the
110+ * LLM is constructed
111+ * @covers ::build_full_ai_prompt
112+ */
113+ public function test_build_full_ai_prompt () :void {
114+ $ this ->resetAfterTest ();
115+
116+ $ question = qtype_aitext_test_helper::make_aitext_question ([]);
117+ set_config ('prompt ' , 'in [responsetext] ' ,'qtype_aitext ' );
118+ set_config ('defaultprompt ' , 'check this ' ,'qtype_aitext ' );
119+ set_config ('markscheme ' , 'one mark ' ,'qtype_aitext ' );
120+ set_config ('jsonprompt ' , 'testprompt ' ,'qtype_aitext ' );
121+
122+ $ response = '<p> Thank you </p> ' ;
123+ $ result = $ question ->build_full_ai_prompt ($ response , $ aiprompt , $ defaultmark , $ markscheme );
124+
125+ $ this ->assertStringContainsString ('[[ Thank you ]] ' , $ result );
126+ // HTML tags should be stripped out.
127+ $ this ->assertStringNotContainsString ('<p> ' , $ result );
128+
129+ $ markscheme = "2 points " ;
130+ $ result = $ question ->build_full_ai_prompt ($ response , $ aiprompt , $ defaultmark , $ markscheme );
131+ $ this ->assertStringContainsString ('2 points ' , $ result );
132+ }
133+
106134 /**
107135 * Check that non valid json returned from the LLM is
108136 * dealt with gracefully
0 commit comments