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;
|
|
|
18 |
|
|
|
19 |
use core_ai\aiactions\responses\response_generate_image;
|
|
|
20 |
use core_ai\aiactions\generate_image;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Test generate_image 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\base
|
|
|
29 |
*/
|
|
|
30 |
final class generate_image_test extends \advanced_testcase {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Test constructor method.
|
|
|
34 |
*/
|
|
|
35 |
public function test_constructor(): void {
|
|
|
36 |
$contextid = 1;
|
|
|
37 |
$userid = 1;
|
|
|
38 |
$prompttext = 'This is a test prompt';
|
|
|
39 |
$aspectratio = 'square';
|
|
|
40 |
$quality = 'hd';
|
|
|
41 |
$numimages = 1;
|
|
|
42 |
$style = 'vivid';
|
|
|
43 |
$action = new generate_image(
|
|
|
44 |
contextid: $contextid,
|
|
|
45 |
userid: $userid,
|
|
|
46 |
prompttext: $prompttext,
|
|
|
47 |
quality: $quality,
|
|
|
48 |
aspectratio: $aspectratio,
|
|
|
49 |
numimages: $numimages,
|
|
|
50 |
style: $style
|
|
|
51 |
);
|
|
|
52 |
|
|
|
53 |
$this->assertEquals($userid, $action->get_configuration('userid'));
|
|
|
54 |
$this->assertEquals($prompttext, $action->get_configuration('prompttext'));
|
|
|
55 |
$this->assertEquals($aspectratio, $action->get_configuration('aspectratio'));
|
|
|
56 |
$this->assertEquals($quality, $action->get_configuration('quality'));
|
|
|
57 |
$this->assertEquals($numimages, $action->get_configuration('numimages'));
|
|
|
58 |
$this->assertEquals($style, $action->get_configuration('style'));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Test store method.
|
|
|
63 |
*/
|
|
|
64 |
public function test_store(): void {
|
|
|
65 |
$this->resetAfterTest();
|
|
|
66 |
global $DB;
|
|
|
67 |
|
|
|
68 |
$contextid = 1;
|
|
|
69 |
$userid = 1;
|
|
|
70 |
$prompttext = 'This is a test prompt';
|
|
|
71 |
$aspectratio = 'square';
|
|
|
72 |
$quality = 'hd';
|
|
|
73 |
$numimages = 1;
|
|
|
74 |
$style = 'vivid';
|
|
|
75 |
$action = new generate_image(
|
|
|
76 |
contextid: $contextid,
|
|
|
77 |
userid: $userid,
|
|
|
78 |
prompttext: $prompttext,
|
|
|
79 |
quality: $quality,
|
|
|
80 |
aspectratio: $aspectratio,
|
|
|
81 |
numimages: $numimages,
|
|
|
82 |
style: $style
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
$body = [
|
|
|
86 |
'revisedprompt' => 'This is a revised prompt',
|
|
|
87 |
'sourceurl' => 'https://example.com/image.png',
|
|
|
88 |
'model' => 'dall-e-3',
|
|
|
89 |
];
|
|
|
90 |
$actionresponse = new response_generate_image(
|
|
|
91 |
success: true,
|
|
|
92 |
);
|
|
|
93 |
$actionresponse->set_response_data($body);
|
|
|
94 |
|
|
|
95 |
$storeid = $action->store($actionresponse);
|
|
|
96 |
|
|
|
97 |
// Check the stored record.
|
|
|
98 |
$record = $DB->get_record('ai_action_generate_image', ['id' => $storeid]);
|
|
|
99 |
$this->assertEquals($prompttext, $record->prompt);
|
|
|
100 |
$this->assertEquals($numimages, $record->numberimages);
|
|
|
101 |
$this->assertEquals($quality, $record->quality);
|
|
|
102 |
$this->assertEquals($aspectratio, $record->aspectratio);
|
|
|
103 |
$this->assertEquals($style, $record->style);
|
|
|
104 |
$this->assertEquals($body['sourceurl'], $record->sourceurl);
|
|
|
105 |
$this->assertEquals($body['revisedprompt'], $record->revisedprompt);
|
|
|
106 |
}
|
|
|
107 |
}
|