2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class UploadTest extends ShimmiePHPUnitTestCase
|
|
|
|
{
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testUploadPage(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->log_in_as_user();
|
|
|
|
|
|
|
|
$this->get_page("upload");
|
|
|
|
$this->assert_title("Upload");
|
|
|
|
}
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
// Because $this->post_image() sends the event directly
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRawUpload(): void
|
2020-10-28 20:51:34 +00:00
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$this->log_in_as_user();
|
|
|
|
$_FILES = [
|
|
|
|
'data0' => [
|
|
|
|
'name' => ['puppy-hugs.jpg'],
|
|
|
|
'type' => ['image/jpeg'],
|
|
|
|
'tmp_name' => ['tests/bedroom_workshop.jpg'],
|
|
|
|
'error' => [0],
|
|
|
|
'size' => [271386],
|
|
|
|
],
|
|
|
|
'data1' => [
|
|
|
|
'name' => ['cat-hugs-2.jpg', 'cat-hugs-3.jpg', 'cat-hugs.jpg'],
|
|
|
|
'type' => ['image/png', 'image/jpeg', 'image/svg'],
|
|
|
|
'tmp_name' => ['tests/favicon.png', 'tests/pbx_screenshot.jpg', 'tests/test.svg'],
|
|
|
|
'error' => [0, 0, 0],
|
|
|
|
'size' => [110361, 64021, 421410],
|
|
|
|
],
|
|
|
|
'data2' => [
|
|
|
|
'name' => [''],
|
|
|
|
'type' => [''],
|
|
|
|
'tmp_name' => [''],
|
|
|
|
'error' => [4],
|
|
|
|
'size' => [0],
|
|
|
|
]
|
|
|
|
];
|
2023-11-11 21:49:12 +00:00
|
|
|
$page = $this->post_page("upload", ["tags0" => "foo bar"]);
|
2020-10-28 20:51:34 +00:00
|
|
|
$this->assert_response(302);
|
|
|
|
$this->assertEquals(4, $database->get_one("SELECT COUNT(*) FROM images"));
|
2024-01-15 22:56:06 +00:00
|
|
|
// FIXME: image IDs get allocated even when transactions are rolled back,
|
|
|
|
// so these IDs are not necessarily correct
|
|
|
|
// $this->assertStringStartsWith("/test/post/list/id%3D4%2C3%2C2%2C1/1", $page->redirect);
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testUpload(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->log_in_as_user();
|
2020-01-29 20:22:50 +00:00
|
|
|
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
|
|
|
|
$this->assertGreaterThan(0, $image_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
|
|
|
|
$this->get_page("post/view/$image_id");
|
|
|
|
$this->assert_title("Post $image_id: computer pbx screenshot");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRejectDupe(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
|
|
|
|
|
2024-01-09 21:01:10 +00:00
|
|
|
$e = $this->assertException(UploadException::class, function () {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
|
2024-01-09 21:01:10 +00:00
|
|
|
});
|
|
|
|
$this->assertStringContainsString("already has hash", $e->getMessage());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRejectUnknownFiletype(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-05 13:53:21 +00:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->post_image("index.php", "test");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function testRejectHuge(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
// FIXME: huge.dat is rejected for other reasons; manual testing shows that this works
|
2024-02-20 00:22:25 +00:00
|
|
|
file_put_contents("data/huge.jpg", \Safe\file_get_contents("tests/pbx_screenshot.jpg") . str_repeat("U", 1024 * 1024 * 3));
|
2024-01-09 21:01:10 +00:00
|
|
|
$e = $this->assertException(UploadException::class, function () {
|
2020-01-28 21:19:59 +00:00
|
|
|
$this->post_image("data/huge.jpg", "test");
|
2024-01-09 21:01:10 +00:00
|
|
|
});
|
2020-01-28 21:19:59 +00:00
|
|
|
unlink("data/huge.jpg");
|
2024-01-09 21:01:10 +00:00
|
|
|
$this->assertEquals("File too large (3.0MB > 1.0MB)", $e->getMessage());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2009-07-15 01:42:18 +00:00
|
|
|
}
|