1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\PartialUserforms\Models\PartialFieldSubmission; |
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\Security\DefaultAdminService; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
11
|
|
|
|
12
|
|
|
class PartialFieldSubmissionTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $usesDatabase = true; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PartialFieldSubmission |
21
|
|
|
*/ |
22
|
|
|
protected $field; |
23
|
|
|
|
24
|
|
|
public function testCanView() |
25
|
|
|
{ |
26
|
|
|
$this->assertTrue($this->field->canView()); |
27
|
|
|
|
28
|
|
|
$member = DefaultAdminService::singleton()->findOrCreateAdmin('[email protected]'); |
29
|
|
|
$this->logInAs($member); |
30
|
|
|
|
31
|
|
|
$this->assertTrue($this->field->canView($member)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testCanCreate() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
Member::currentUser()->logOut(); |
|
|
|
|
37
|
|
|
$this->assertFalse($this->field->canCreate()); |
38
|
|
|
|
39
|
|
|
$member = DefaultAdminService::singleton()->findOrCreateAdmin('[email protected]'); |
40
|
|
|
$this->logInAs($member); |
41
|
|
|
|
42
|
|
|
$this->assertTrue($this->field->canCreate($member)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testCanEdit() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
Member::currentUser()->logOut(); |
|
|
|
|
48
|
|
|
$this->assertFalse($this->field->canEdit()); |
49
|
|
|
|
50
|
|
|
$member = DefaultAdminService::singleton()->findOrCreateAdmin('[email protected]'); |
51
|
|
|
$this->logInAs($member); |
52
|
|
|
|
53
|
|
|
$this->assertTrue($this->field->canEdit($member)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testCanDelete() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
Member::currentUser()->logOut(); |
|
|
|
|
59
|
|
|
$this->assertFalse($this->field->canDelete()); |
60
|
|
|
|
61
|
|
|
$member = DefaultAdminService::singleton()->findOrCreateAdmin('[email protected]'); |
62
|
|
|
$this->logInAs($member); |
63
|
|
|
|
64
|
|
|
$this->assertTrue($this->field->canDelete($member)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function setUp() |
68
|
|
|
{ |
69
|
|
|
$this->field = PartialFieldSubmission::create(); |
70
|
|
|
$partialForm = PartialFormSubmission::create(); |
71
|
|
|
$udf = UserDefinedForm::create(['Title' => 'Test'])->write(); |
72
|
|
|
$partialForm->UserDefinedFormID = $udf; |
73
|
|
|
$partialForm->UserDefinedFormClass = UserDefinedForm::class; |
|
|
|
|
74
|
|
|
$partialFormID = $partialForm->write(); |
75
|
|
|
$partialForm->publishRecursive(); |
|
|
|
|
76
|
|
|
$this->field->SubmittedFormID = $partialFormID; |
77
|
|
|
// testCanCreate() will most likely be an error due to SubmittedForm returning a parent's permission |
78
|
|
|
// without checking if Parent exists, so have to set the SubmittedForm parent |
79
|
|
|
$this->field->SubmittedForm()->ParentID = $udf; |
|
|
|
|
80
|
|
|
|
81
|
|
|
return parent::setUp(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function tearDown() |
85
|
|
|
{ |
86
|
|
|
parent::tearDown(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.