1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_ai\aiactions\responses;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use core_ai\aiactions\responses\response_generate_image;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Test response_generate_image_test action methods.
|
|
|
24 |
*
|
|
|
25 |
* @package core_ai
|
|
|
26 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core_ai\aiactions\responses\response_generate_image
|
|
|
29 |
*/
|
|
|
30 |
final class response_generate_image_test extends \advanced_testcase {
|
|
|
31 |
/**
|
|
|
32 |
* Test get_basename.
|
|
|
33 |
*/
|
|
|
34 |
public function test_get_success(): void {
|
|
|
35 |
$actionresponse = new response_generate_image(
|
|
|
36 |
success: true,
|
|
|
37 |
);
|
|
|
38 |
|
|
|
39 |
$this->assertTrue($actionresponse->get_success());
|
|
|
40 |
$this->assertEquals('generate_image', $actionresponse->get_actionname());
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test constructor with error.
|
|
|
45 |
*/
|
|
|
46 |
public function test_construct_error(): void {
|
|
|
47 |
$this->expectException(coding_exception::class);
|
|
|
48 |
$this->expectExceptionMessage('Error code and message must exist in an error response.');
|
|
|
49 |
new response_generate_image(
|
|
|
50 |
success: false,
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Test set_response_data.
|
|
|
56 |
*/
|
|
|
57 |
public function test_set_response_data(): void {
|
|
|
58 |
$this->resetAfterTest();
|
|
|
59 |
|
|
|
60 |
// Create a file to store.
|
|
|
61 |
$fs = get_file_storage();
|
|
|
62 |
$filerecord = new \stdClass();
|
|
|
63 |
$filerecord->contextid = 1;
|
|
|
64 |
$filerecord->component = 'core_ai';
|
|
|
65 |
$filerecord->filearea = 'draft';
|
|
|
66 |
$filerecord->itemid = 0;
|
|
|
67 |
$filerecord->filepath = '/';
|
|
|
68 |
$filerecord->filename = 'test.txt';
|
|
|
69 |
$file = $fs->create_file_from_string($filerecord, 'This is a test file');
|
|
|
70 |
$body = [
|
|
|
71 |
'draftfile' => $file,
|
|
|
72 |
'revisedprompt' => 'This is a revised prompt',
|
|
|
73 |
'sourceurl' => 'https://example.com/image.png',
|
|
|
74 |
];
|
|
|
75 |
$actionresponse = new response_generate_image(
|
|
|
76 |
success: true,
|
|
|
77 |
);
|
|
|
78 |
$actionresponse->set_response_data($body);
|
|
|
79 |
|
|
|
80 |
$this->assertEquals($file, $actionresponse->get_response_data()['draftfile']);
|
|
|
81 |
$this->assertEquals('This is a revised prompt', $actionresponse->get_response_data()['revisedprompt']);
|
|
|
82 |
$this->assertEquals('https://example.com/image.png', $actionresponse->get_response_data()['sourceurl']);
|
|
|
83 |
}
|
|
|
84 |
}
|