mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2026-06-07 04:28:11 +08:00
将 v8 重构分支中残留的 ThinkAdminDeveloper 文本统一调整为 ThinkAdmin,避免迁移到主仓库后继续暴露旧开发仓库名称。 主要内容: - 更新 README 标题与项目描述。 - 统一 PHP 文件头注释中的项目标识。 - 同步调整测试、配置、插件与文档中的旧仓库名称文本。 - 保持旧包删除说明与架构边界测试语义不变,只清理品牌名称残留。
212 lines
7.8 KiB
PHP
212 lines
7.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* +----------------------------------------------------------------------
|
|
* | ThinkAdmin Plugin
|
|
* +----------------------------------------------------------------------
|
|
* | Copyright (c) 2014~2026 ThinkAdmin [ thinkadmin.top ]
|
|
* +----------------------------------------------------------------------
|
|
* | Official Website: https://thinkadmin.top
|
|
* +----------------------------------------------------------------------
|
|
* | Licensed: https://mit-license.org
|
|
* | Disclaimer: https://thinkadmin.top/disclaimer
|
|
* | Vip Rights: https://thinkadmin.top/vip-introduce
|
|
* +----------------------------------------------------------------------
|
|
* | Gitee Repository: https://gitee.com/zoujingli/ThinkAdmin
|
|
* | Github Repository: https://github.com/zoujingli/ThinkAdmin
|
|
* +----------------------------------------------------------------------
|
|
*/
|
|
|
|
namespace think\admin\tests;
|
|
|
|
use plugin\system\controller\api\Upload as UploadController;
|
|
use plugin\system\service\AuthService;
|
|
use plugin\system\service\SystemContext as PluginSystemContext;
|
|
use think\admin\contract\SystemContextInterface;
|
|
use think\admin\runtime\RequestContext;
|
|
use think\admin\tests\Support\SqliteIntegrationTestCase;
|
|
use think\Container;
|
|
use think\exception\HttpResponseException;
|
|
use think\Request;
|
|
|
|
/**
|
|
* @internal
|
|
* @coversNothing
|
|
*/
|
|
class UploadControllerTest extends SqliteIntegrationTestCase
|
|
{
|
|
public function testImageRendersHtmlDialogWithoutTemplatePlaceholders(): void
|
|
{
|
|
$html = $this->callHtmlController('image', [
|
|
'id' => 'upload-trigger',
|
|
'file' => 'images',
|
|
'type' => 'png,jpg',
|
|
'path' => 'system/example',
|
|
'size' => 4096,
|
|
], 9101);
|
|
|
|
$this->assertStringContainsString('id="ImageDialog"', $html);
|
|
$this->assertStringContainsString('var config = {"buttonSelector":"#upload-trigger"', $html);
|
|
$this->assertStringContainsString('"multiple":true', $html);
|
|
$this->assertStringContainsString('"imageUrl":"/api/system/upload/image"', $html);
|
|
$this->assertStringContainsString('data-file="mul"', $html);
|
|
$this->assertStringContainsString('data-path="system/example"', $html);
|
|
$this->assertStringContainsString("$.form.load(config.imageUrl, this.params, 'get'", $html);
|
|
$this->assertStringContainsString('centerLayer: function () {', $html);
|
|
$this->assertStringContainsString("\$layer.css({left: left + 'px', top: top + 'px', marginLeft: 0, marginTop: 0});", $html);
|
|
$this->assertStringContainsString('上传图片', $html);
|
|
$this->assertStringNotContainsString('{$get', $html);
|
|
$this->assertStringNotContainsString('{:url', $html);
|
|
$this->assertStringNotContainsString('{if ', $html);
|
|
}
|
|
|
|
public function testIndexBuildsUploadScriptUsingTokenRestrictedExtensions(): void
|
|
{
|
|
$response = $this->callIndexController([
|
|
'uptoken' => AuthService::withUploadToken(321, 'png,pdf,mp4'),
|
|
]);
|
|
|
|
$contentType = strval($response->getHeader('Content-Type'));
|
|
$content = strval($response->getContent());
|
|
|
|
$this->assertStringContainsString('application/x-javascript', $contentType);
|
|
$this->assertStringContainsString('"png":"image\/png"', $content);
|
|
$this->assertStringContainsString('"mp4":"video\/mp4"', $content);
|
|
$this->assertStringNotContainsString('"jpg":"image\/jpeg"', $content);
|
|
$this->assertStringNotContainsString('"pdf":"application\/pdf"', $content);
|
|
$this->assertStringContainsString("indexOf('date') > -1", $content);
|
|
}
|
|
|
|
public function testDoneMarksOwnedFileAsUploaded(): void
|
|
{
|
|
$file = $this->createSystemFileFixture([
|
|
'uuid' => 9101,
|
|
'status' => 1,
|
|
'hash' => 'upload-hash-owned',
|
|
]);
|
|
|
|
$result = $this->callActionController('done', [
|
|
'id' => $file->getAttr('id'),
|
|
'hash' => 'upload-hash-owned',
|
|
], 9101);
|
|
|
|
$file = $file->refresh();
|
|
|
|
$this->assertSame(200, intval($result['code'] ?? 0));
|
|
$this->assertStringContainsString('文件记录状态更新成功!', $result['info'] ?? '');
|
|
$this->assertSame(2, intval($file->getAttr('status')));
|
|
}
|
|
|
|
public function testDoneRejectsFileFromAnotherAdminUser(): void
|
|
{
|
|
$file = $this->createSystemFileFixture([
|
|
'uuid' => 9201,
|
|
'status' => 1,
|
|
'hash' => 'upload-hash-foreign',
|
|
]);
|
|
|
|
$result = $this->callActionController('done', [
|
|
'id' => $file->getAttr('id'),
|
|
'hash' => 'upload-hash-foreign',
|
|
], 9101);
|
|
|
|
$file = $file->refresh();
|
|
|
|
$this->assertSame(500, intval($result['code'] ?? 1));
|
|
$this->assertStringContainsString('文件记录不存在!', $result['info'] ?? '');
|
|
$this->assertSame(1, intval($file->getAttr('status')));
|
|
}
|
|
|
|
protected function defineSchema(): void
|
|
{
|
|
$this->createSystemFileTable();
|
|
}
|
|
|
|
protected function afterSchemaCreated(): void
|
|
{
|
|
$context = new PluginSystemContext();
|
|
Container::getInstance()->instance(SystemContextInterface::class, $context);
|
|
$this->app->instance(SystemContextInterface::class, $context);
|
|
$this->configureView();
|
|
}
|
|
|
|
private function callIndexController(array $query = [])
|
|
{
|
|
$request = (new Request())
|
|
->withGet($query)
|
|
->withServer(['HTTPS' => 'on'])
|
|
->setHost('admin.example.com')
|
|
->setMethod('GET')
|
|
->setController('upload')
|
|
->setAction('index');
|
|
|
|
$this->setRequestPayload($request, $query);
|
|
$this->app->instance('request', $request);
|
|
|
|
$controller = new UploadController($this->app);
|
|
|
|
return $controller->index();
|
|
}
|
|
|
|
private function callHtmlController(string $action, array $query = [], int $userId = 9101): string
|
|
{
|
|
$request = (new Request())
|
|
->withGet($query)
|
|
->withPost($query)
|
|
->setMethod('GET')
|
|
->setController('upload')
|
|
->setAction($action);
|
|
|
|
$this->bindAdminUser($userId);
|
|
$this->setRequestPayload($request, $query);
|
|
$this->app->instance('request', $request);
|
|
|
|
try {
|
|
$controller = new UploadController($this->app);
|
|
$controller->{$action}();
|
|
self::fail("Expected UploadController::{$action} to throw HttpResponseException.");
|
|
} catch (HttpResponseException $exception) {
|
|
return strval($exception->getResponse()->getContent());
|
|
}
|
|
}
|
|
|
|
private function callActionController(string $action, array $payload, int $userId): array
|
|
{
|
|
$request = (new Request())
|
|
->withGet($payload)
|
|
->withPost($payload)
|
|
->setMethod('POST')
|
|
->setController('upload')
|
|
->setAction($action);
|
|
|
|
$this->bindAdminUser($userId);
|
|
$this->setRequestPayload($request, $payload);
|
|
$this->app->instance('request', $request);
|
|
|
|
try {
|
|
$controller = new UploadController($this->app);
|
|
$controller->{$action}();
|
|
self::fail("Expected UploadController::{$action} to throw HttpResponseException.");
|
|
} catch (HttpResponseException $exception) {
|
|
return json_decode($exception->getResponse()->getContent(), true) ?: [];
|
|
}
|
|
}
|
|
|
|
private function bindAdminUser(int $userId): void
|
|
{
|
|
RequestContext::instance()->setAuth([
|
|
'id' => $userId,
|
|
'username' => "admin-{$userId}",
|
|
'password' => $this->hashSystemPassword('changed-password'),
|
|
], '', true);
|
|
}
|
|
|
|
private function setRequestPayload(Request $request, array $data): void
|
|
{
|
|
$property = new \ReflectionProperty(Request::class, 'request');
|
|
$property->setAccessible(true);
|
|
$property->setValue($request, $data);
|
|
}
|
|
}
|