CaiWuXiTong/tests/Feature/Business/CourierProfileEditingTest.php

157 lines
8.5 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Business\CourierProfileData;
use App\Models\Auth\Permission;
use App\Models\Auth\Role;
use App\Models\Business\CourierOnboarding;
use App\Models\Business\CourierProfile;
use App\Models\Business\CourierProfileEvent;
use App\Utilities\CourierProfilePermissions;
use Illuminate\Support\Facades\Http;
use Mockery;
use Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository;
use Tests\Feature\FeatureTestCase;
class CourierProfileEditingTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
Http::preventStrayRequests();
$mirror = Mockery::mock(CourierInformationMirrorRepository::class);
$mirror->shouldReceive('available')->andReturnTrue();
$mirror->shouldReceive('ready')->andReturnTrue();
$mirror->shouldReceive('relatedAvailability')->andReturn([]);
$mirror->shouldReceive('state')->andReturn(['status' => 'succeeded', 'mirror_row_count' => 1902]);
$mirror->shouldReceive('syncStatus')->andReturn(['code' => 'healthy']);
$mirror->shouldReceive('paginate')->andReturn(['data' => [$this->row()], 'total' => 1902]);
$mirror->shouldReceive('exportRows')->andReturnUsing(function (): \Generator { yield $this->row(); });
$this->app->instance(CourierInformationMirrorRepository::class, $mirror);
}
public function test_ungranted_administrator_cannot_edit_using_page_or_direct_post(): void
{
$this->withExceptionHandling()->loginAs()->get($this->page(['selected' => 'TEST-EDIT', 'edit' => 1]))
->assertOk()->assertDontSee('aria-label="编辑快递资料"', false)->assertDontSee('data-courier-edit-open', false);
$this->post(route('business.courier-profiles.update', 'TEST-EDIT'), ['expname' => '越权修改'])
->assertForbidden();
$this->assertSame(0, CourierProfile::count());
$this->assertSame(0, CourierProfileEvent::count());
}
public function test_granted_editor_can_save_business_fields_and_export_the_same_values(): void
{
$this->grant();
$values = [
'expname' => '编辑后快递', 'exp_properties' => '工厂', 'outcode' => 'EXT-20', 'scexpcode' => 'SC-20',
'tradebody' => '测试乙方', 'signcom' => '测试甲方', 'addvalidsgn' => '1', 'autonosgn' => '1',
'packnosgn' => '0', 'skucountsplitsgn' => '1', 'weight0sgn' => '0', 'volume0sgn' => '1',
'handnosgn' => '0', 'decryptsgn' => '1', 'entity_type' => 'company', 'company_full_name' => '资料主体',
'network_code' => 'NET-20', 'courier_email' => 'courier@example.test', 'courier_category' => '内采快递',
'shipping_address' => '测试发货地址', 'status' => '1',
'Id' => 999, 'expcode' => 'OTHER', 'modified' => '2099-01-01', 'onboarding_status' => 'activated',
];
$this->loginAs()->get($this->page())
->assertOk()->assertSee('data-courier-edit-open', false);
$this->get($this->page(['selected' => 'TEST-EDIT', 'edit' => 1]))
->assertOk()->assertSee('name="outcode"', false)->assertSee('name="network_code"', false);
$this->post(route('business.courier-profiles.update', 'TEST-EDIT'), $values)
->assertSessionHasNoErrors()->assertRedirect();
$profile = CourierProfile::sole();
$this->assertSame('TEST-EDIT', $profile->expcode);
$this->assertSame('EXT-20', $profile->overrides['outcode']);
$this->assertArrayNotHasKey('Id', $profile->overrides);
$this->assertArrayNotHasKey('modified', $profile->overrides);
$this->assertArrayNotHasKey('onboarding_status', $profile->overrides);
$event = CourierProfileEvent::sole();
$this->assertSame((int) $this->user->id, (int) $event->created_by);
$this->assertSame(['label' => '外部编码', 'old' => 'EXT-OLD', 'new' => 'EXT-20'], $event->changes['outcode']);
$this->get($this->page(['selected' => 'TEST-EDIT', 'edit' => 1]))
->assertOk()->assertSee('value="EXT-20"', false)->assertSeeText('NET-20')->assertSeeText('资料主体');
$export = $this->get(route('business.courier-payables.courier-information.export'))->assertOk();
$lines = preg_split('/\r?\n/', ltrim($export->streamedContent(), "\xEF\xBB\xBF"));
$record = array_combine(str_getcsv($lines[0]), str_getcsv($lines[1]));
$this->assertSame('EXT-20', $record['外部编码']);
$this->assertSame('NET-20', $record['网点代码']);
$this->assertSame('公司 · 资料主体', $record['入驻主体']);
$this->assertSame('1', $record['ID']);
}
public function test_limited_editor_needs_explicit_page_access_and_can_submit_on_first_request(): void
{
$this->user->syncRoles([Role::where('name', 'user')->firstOrFail()->id]);
$this->user->update(['created_from' => 'dingtalk_roster_limited']);
config(['dingtalk.limited_login_page_keys' => []]);
$this->grant();
$this->assertFalse(CourierProfilePermissions::canEdit($this->user));
$subject = 'user_' . $this->user->id;
setting()->set('system.sidebar_permissions', ['configured' => [$subject], 'subjects' => [$subject => ['courier-payables/courier-information']]]);
setting()->save();
$this->assertTrue(CourierProfilePermissions::canEdit($this->user));
$this->withExceptionHandling()->loginAs();
$page = $this->page(['selected' => 'TEST-EDIT', 'edit' => 1]);
$update = route('business.courier-profiles.update', 'TEST-EDIT');
\App\Models\Common\Company::forgetCurrent();
$this->get($page)->assertOk();
\App\Models\Common\Company::forgetCurrent();
$this->post($update, ['expname' => '有限账号修改'])
->assertSessionHasNoErrors()->assertRedirect();
$this->get(route('system-settings.users.index'))->assertForbidden();
}
public function test_disabled_or_non_member_editor_is_denied(): void
{
$this->grant();
$this->assertTrue(CourierProfilePermissions::canEdit($this->user));
$this->user->enabled = false;
$this->assertFalse(CourierProfilePermissions::canEdit($this->user));
$this->user->enabled = true;
$this->user->companies()->detach();
$this->assertFalse(CourierProfilePermissions::canEdit($this->user));
}
public function test_invalid_values_are_rejected_and_unchanged_values_do_not_create_progress(): void
{
$this->grant();
$this->withExceptionHandling()->loginAs()->from($this->page(['selected' => 'TEST-EDIT', 'edit' => 1]))
->post(route('business.courier-profiles.update', 'TEST-EDIT'), ['form' => 'edit', 'expname' => '测试快递', 'autonosgn' => 'yes', 'courier_email' => 'bad-email', 'stopdate' => 'not-date'])
->assertSessionHasErrors(['autonosgn', 'courier_email', 'stopdate']);
$this->assertSame(0, CourierProfile::count());
$this->post(route('business.courier-profiles.update', 'TEST-EDIT'), ['expname' => '测试快递', 'outcode' => 'EXT-OLD', 'addvalidsgn' => null])->assertSessionHasNoErrors();
$this->assertSame(0, CourierProfileEvent::count());
}
public function test_onboarding_source_is_prefilled_and_remains_unchanged(): void
{
$onboarding = new CourierOnboarding(['company_full_name' => '原始主体', 'entity_type' => 'company', 'network_code' => 'NET-OLD', 'deduction_method' => '运费']);
$row = $this->row();
$row->_info = ['onboarding' => $onboarding];
$data = app(CourierProfileData::class);
$filled = $data->apply($row, []);
$this->assertSame('NET-OLD', $filled->network_code);
$edited = $data->apply($row, ['network_code' => 'NET-NEW', 'company_full_name' => '新资料主体']);
$this->assertSame('NET-NEW', $edited->network_code);
$this->assertSame('NET-OLD', $onboarding->network_code);
$this->assertSame('原始主体', $onboarding->company_full_name);
}
private function grant(): void
{
setting()->set(CourierProfilePermissions::SETTING_KEY, json_encode([$this->user->id]));
setting()->save();
}
private function row(): object
{
return (object) ['Id' => 1, 'expcode' => 'TEST-EDIT', 'expname' => '测试快递', 'outcode' => 'EXT-OLD', 'status' => '1'];
}
private function page(array $query = []): string
{
return route('business.pages.show', ['section' => 'courier-payables', 'page' => 'courier-information', ...$query]);
}
}