diff --git a/module/product/test/lib/product.unittest.class.php b/module/product/test/lib/product.unittest.class.php index ed6fc3a14a..2cc3dc44db 100644 --- a/module/product/test/lib/product.unittest.class.php +++ b/module/product/test/lib/product.unittest.class.php @@ -3150,4 +3150,75 @@ class productTest return $result; } + + /** + * Test responseAfterEdit method. + * + * @param int $productID + * @param int $programID + * @param string $hookMessage + * @access public + * @return array + */ + public function responseAfterEditTest(int $productID, int $programID, string $hookMessage = ''): array + { + global $tester; + // 创建productZen实例 + $productZen = new productZen(); + $productZen->lang = $tester->lang; + $productZen->config = $tester->config; + $productZen->app = $tester->app; + $productZen->session = $tester->session; + + // 创建一个扩展的productZen类来模拟executeHooks和getEditedLocate方法 + $extendedProductZen = new class($productZen, $hookMessage) extends productZen { + private $mockHookMessage; + private $originalZen; + + public function __construct($originalZen, $hookMessage) + { + $this->originalZen = $originalZen; + $this->mockHookMessage = $hookMessage; + $this->lang = $originalZen->lang; + $this->config = $originalZen->config; + $this->app = $originalZen->app; + $this->session = $originalZen->session; + $this->moduleName = 'product'; + } + + public function executeHooks(int $objectID): string + { + return $this->mockHookMessage ?: ''; + } + + protected function getEditedLocate($productID, $programID): array + { + $moduleName = $programID ? 'program' : 'product'; + $methodName = $programID ? 'product' : 'view'; + $param = $programID ? "programID=$programID" : "product=$productID"; + $location = "test_link_{$moduleName}_{$methodName}"; + + if(!$programID) { + // 模拟session设置 + $this->session->set('productList', "test_link_product_browse", 'product'); + } + + return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $location); + } + + public function createLink(string $moduleName, string $methodName = 'index', array|string $vars = [], string $viewType = '', bool $onlybody = false): string + { + return "test_link_{$moduleName}_{$methodName}"; + } + }; + + // 使用反射调用被测试的protected方法 + $method = $this->objectZen->getMethod('responseAfterEdit'); + $method->setAccessible(true); + + $result = $method->invokeArgs($extendedProductZen, array($productID, $programID)); + if(dao::isError()) return dao::getError(); + + return $result; + } } diff --git a/module/product/test/zen/responseafteredit.php b/module/product/test/zen/responseafteredit.php new file mode 100755 index 0000000000..dfb90604eb --- /dev/null +++ b/module/product/test/zen/responseafteredit.php @@ -0,0 +1,49 @@ +#!/usr/bin/env php +id->range('1-10'); +$table->name->range('产品A,产品B,产品C'); +$table->status->range('normal'); +$table->program->range('0,1,2'); +$table->gen(5); + +// 3. 用户登录(选择合适角色) +su('admin'); + +// 4. 创建测试实例(变量名与模块名一致) +$productTest = new productTest(); + +// 5. 🔴 强制要求:必须包含至少5个测试步骤 +r($productTest->responseAfterEditTest(1, 0, '')) && p('result,load') && e('success,test_link_product_view'); // 步骤1:正常产品编辑,无钩子消息 +r($productTest->responseAfterEditTest(2, 1, '')) && p('result,load') && e('success,test_link_program_product'); // 步骤2:程序集产品编辑,无钩子消息 +r($productTest->responseAfterEditTest(3, 0, '自定义钩子消息')) && p('result,message') && e('success,自定义钩子消息'); // 步骤3:正常产品编辑,有钩子消息 +r($productTest->responseAfterEditTest(4, 2, '钩子执行成功')) && p('result,load') && e('success,test_link_program_product'); // 步骤4:程序集产品编辑,有钩子消息 +r($productTest->responseAfterEditTest(0, 0, '')) && p('result') && e('success'); // 步骤5:无效productID测试 \ No newline at end of file