Skip to content

Commit 8ce1e5e

Browse files
committed
add form tests
1 parent 5fbafd2 commit 8ce1e5e

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

tests/Feature/Fieldtypes/RelationshipFieldtypeTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\Attributes\Test;
66
use Statamic\Facades\Collection;
77
use Statamic\Facades\Entry;
8+
use Statamic\Facades\Form;
89
use Statamic\Facades\Nav;
910
use Statamic\Facades\Role;
1011
use Statamic\Facades\Taxonomy;
@@ -516,6 +517,115 @@ public function a_super_admin_sees_all_structures()
516517
$this->assertArrayNotHasKey('invalid', $data['main']);
517518
$this->assertArrayNotHasKey('invalid', $data['collection::pages']);
518519
}
520+
521+
#[Test]
522+
public function it_scopes_form_listing_to_viewable_forms()
523+
{
524+
Form::make('contact')->title('Contact')->save();
525+
Form::make('secret')->title('Secret')->save();
526+
527+
$this->setTestRoles(['test' => ['access cp', 'view contact form submissions']]);
528+
$user = User::make()->assignRole('test')->save();
529+
530+
$config = base64_encode(json_encode(['type' => 'form']));
531+
532+
$response = $this
533+
->actingAs($user)
534+
->getJson("/cp/fieldtypes/relationship?config={$config}")
535+
->assertOk();
536+
537+
$ids = collect($response->json('data'))->pluck('id')->all();
538+
539+
$this->assertContains('contact', $ids);
540+
$this->assertNotContains('secret', $ids);
541+
}
542+
543+
#[Test]
544+
public function it_returns_a_placeholder_for_an_unviewable_form_by_id()
545+
{
546+
Form::make('contact')->title('Contact')->save();
547+
Form::make('secret')->title('Secret')->save();
548+
549+
$this->setTestRoles(['test' => ['access cp', 'view contact form submissions']]);
550+
$user = User::make()->assignRole('test')->save();
551+
552+
$config = base64_encode(json_encode(['type' => 'form']));
553+
554+
$response = $this
555+
->actingAs($user)
556+
->postJson('/cp/fieldtypes/relationship/data', [
557+
'config' => $config,
558+
'selections' => ['contact', 'secret'],
559+
])
560+
->assertOk();
561+
562+
$data = collect($response->json('data'))->keyBy('id');
563+
564+
$this->assertArrayNotHasKey('invalid', $data['contact']);
565+
$this->assertTrue($data['secret']['invalid']);
566+
$this->assertEquals('secret', $data['secret']['title']);
567+
}
568+
569+
#[Test]
570+
public function the_form_by_id_placeholder_does_not_reveal_whether_a_form_exists()
571+
{
572+
Form::make('secret')->title('Secret')->save();
573+
574+
$this->setTestRoles(['test' => ['access cp']]);
575+
$user = User::make()->assignRole('test')->save();
576+
577+
$config = base64_encode(json_encode(['type' => 'form']));
578+
579+
$response = $this
580+
->actingAs($user)
581+
->postJson('/cp/fieldtypes/relationship/data', [
582+
'config' => $config,
583+
'selections' => ['secret', 'does-not-exist'],
584+
])
585+
->assertOk();
586+
587+
$data = collect($response->json('data'))->keyBy('id');
588+
589+
$this->assertEquals(
590+
['id' => 'secret', 'title' => 'secret', 'invalid' => true],
591+
$data['secret']
592+
);
593+
$this->assertEquals(
594+
['id' => 'does-not-exist', 'title' => 'does-not-exist', 'invalid' => true],
595+
$data['does-not-exist']
596+
);
597+
}
598+
599+
#[Test]
600+
public function a_super_admin_sees_all_forms()
601+
{
602+
Form::make('contact')->title('Contact')->save();
603+
Form::make('secret')->title('Secret')->save();
604+
605+
$config = base64_encode(json_encode(['type' => 'form']));
606+
$user = User::make()->makeSuper()->save();
607+
608+
$listing = $this
609+
->actingAs($user)
610+
->getJson("/cp/fieldtypes/relationship?config={$config}")
611+
->assertOk();
612+
613+
$ids = collect($listing->json('data'))->pluck('id')->all();
614+
$this->assertContains('contact', $ids);
615+
$this->assertContains('secret', $ids);
616+
617+
$byId = $this
618+
->actingAs($user)
619+
->postJson('/cp/fieldtypes/relationship/data', [
620+
'config' => $config,
621+
'selections' => ['contact', 'secret'],
622+
])
623+
->assertOk();
624+
625+
$data = collect($byId->json('data'))->keyBy('id');
626+
$this->assertArrayNotHasKey('invalid', $data['contact']);
627+
$this->assertArrayNotHasKey('invalid', $data['secret']);
628+
}
519629
}
520630

521631
class StartsWithC extends Scope

0 commit comments

Comments
 (0)