package com.ruoyi.other.controller; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.page.PageInfo; import com.ruoyi.other.api.domain.TGroupCompany; import com.ruoyi.other.service.CompanyAccountAccessService; import com.ruoyi.other.service.TGroupCompanyService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** * 公司列表查询接口数据范围测试:公司账号(roleType=5)只查本公司数据。 */ class TGroupCompanyControllerTest { private TGroupCompanyController controller; private CompanyAccountAccessService companyAccountAccessService; @SuppressWarnings("unchecked") private final LambdaQueryChainWrapper chain = (LambdaQueryChainWrapper) mock(LambdaQueryChainWrapper.class); @BeforeEach void setUp() { TGroupCompanyService groupCompanyService = mock(TGroupCompanyService.class); when(groupCompanyService.lambdaQuery()).thenReturn(chain); when(chain.eq(anyBoolean(), any(), any())).thenReturn(chain); when(chain.like(anyBoolean(), any(), any())).thenReturn(chain); when(chain.orderByDesc(any(SFunction.class))).thenReturn(chain); companyAccountAccessService = mock(CompanyAccountAccessService.class); controller = new TGroupCompanyController(); ReflectionTestUtils.setField(controller, "groupCompanyService", groupCompanyService); ReflectionTestUtils.setField(controller, "companyAccountAccessService", companyAccountAccessService); } @Test @DisplayName("公司账号查询公司列表时强制追加本公司ID条件") void companyAccountQueriesOwnCompanyOnly() { when(companyAccountAccessService.getCurrentCompanyId()).thenReturn(9); AjaxResult> result = controller.page(1, 10, null, null, null, null, null); assertNotNull(result); verify(chain).eq(eq(true), any(), eq(9)); } @Test @DisplayName("非公司账号查询公司列表时不追加本公司ID条件") void platformAccountKeepsOriginalScope() { when(companyAccountAccessService.getCurrentCompanyId()).thenReturn(null); AjaxResult> result = controller.page(1, 10, null, null, null, null, null); assertNotNull(result); verify(chain, never()).eq(eq(true), any(), any()); } }