From 68e00f646ef532fc133df37d75396f49cbc6e8d0 Mon Sep 17 00:00:00 2001 From: liugang Date: Fri, 12 Sep 2025 12:01:06 +0800 Subject: [PATCH] + [misc] Add unit tests for searchTao::checkProgramPriv() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../search/test/lib/search.unittest.class.php | 30 ++++++++ module/search/test/tao/checkprogrampriv.php | 72 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 module/search/test/tao/checkprogrampriv.php diff --git a/module/search/test/lib/search.unittest.class.php b/module/search/test/lib/search.unittest.class.php index 2540012b2d..ea04189581 100755 --- a/module/search/test/lib/search.unittest.class.php +++ b/module/search/test/lib/search.unittest.class.php @@ -873,4 +873,34 @@ class searchTest return $result; } + + /** + * Test checkProgramPriv method. + * + * @param array $results + * @param array $objectIdList + * @param string $programs + * @access public + * @return array + */ + public function checkProgramPrivTest(array $results, array $objectIdList, string $programs = '1,2,3'): array + { + // 设置用户权限 + global $tester; + if(!isset($tester->app->user->view)) $tester->app->user->view = new stdClass(); + $tester->app->user->view->programs = $programs; + + // 设置tao对象的app引用 + $this->objectTao->app->user->view = $tester->app->user->view; + + // 使用反射访问私有方法 + $reflection = new ReflectionClass($this->objectTao); + $method = $reflection->getMethod('checkProgramPriv'); + $method->setAccessible(true); + + $result = $method->invokeArgs($this->objectTao, array($results, $objectIdList)); + if(dao::isError()) return dao::getError(); + + return $result; + } } diff --git a/module/search/test/tao/checkprogrampriv.php b/module/search/test/tao/checkprogrampriv.php new file mode 100755 index 0000000000..64907156d4 --- /dev/null +++ b/module/search/test/tao/checkprogrampriv.php @@ -0,0 +1,72 @@ +#!/usr/bin/env php + (object)array('id' => 1, 'title' => '测试结果1', 'objectType' => 'program', 'objectID' => 1), + 2 => (object)array('id' => 2, 'title' => '测试结果2', 'objectType' => 'program', 'objectID' => 2), + 3 => (object)array('id' => 3, 'title' => '测试结果3', 'objectType' => 'program', 'objectID' => 4) +); + +// 测试步骤1:有权限的项目集,应该保留搜索结果 +$objectIdList = array(1 => 1, 2 => 2, 4 => 3); // programID => recordID +r(count($searchTest->checkProgramPrivTest($results, $objectIdList, '1,2,3'))) && p() && e(2); + +// 测试步骤2:无权限的项目集,应该移除搜索结果 +$results = array( + 1 => (object)array('id' => 1, 'title' => '测试结果1', 'objectType' => 'program', 'objectID' => 4), + 2 => (object)array('id' => 2, 'title' => '测试结果2', 'objectType' => 'program', 'objectID' => 5) +); +$objectIdList = array(4 => 1, 5 => 2); +r(count($searchTest->checkProgramPrivTest($results, $objectIdList, '1,2,3'))) && p() && e(0); + +// 测试步骤3:部分有权限的项目集,应该保留有权限的结果 +$results = array( + 1 => (object)array('id' => 1, 'title' => '测试结果1', 'objectType' => 'program', 'objectID' => 1), + 2 => (object)array('id' => 2, 'title' => '测试结果2', 'objectType' => 'program', 'objectID' => 4) +); +$objectIdList = array(1 => 1, 4 => 2); +r(count($searchTest->checkProgramPrivTest($results, $objectIdList, '1,2,3'))) && p() && e(1); + +// 测试步骤4:空的对象ID列表,应该返回原始结果 +$results = array( + 1 => (object)array('id' => 1, 'title' => '测试结果1', 'objectType' => 'program', 'objectID' => 1), + 2 => (object)array('id' => 2, 'title' => '测试结果2', 'objectType' => 'program', 'objectID' => 2), + 3 => (object)array('id' => 3, 'title' => '测试结果3', 'objectType' => 'program', 'objectID' => 3) +); +$objectIdList = array(); +r(count($searchTest->checkProgramPrivTest($results, $objectIdList, '1,2,3'))) && p() && e(3); + +// 测试步骤5:不存在的项目集ID,应该移除对应结果 +$results = array( + 1 => (object)array('id' => 1, 'title' => '测试结果1', 'objectType' => 'program', 'objectID' => 1), + 2 => (object)array('id' => 2, 'title' => '测试结果2', 'objectType' => 'program', 'objectID' => 999) +); +$objectIdList = array(1 => 1, 999 => 2); +r(count($searchTest->checkProgramPrivTest($results, $objectIdList, '1,2,3'))) && p() && e(1); \ No newline at end of file