Completed
Pull Request — master (#12)
by Simon
01:22
created

PartialFieldSubmissionTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
35
    {
36
        Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
46
    {
47
        Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
    {
58
        Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property UserDefinedFormClass does not exist on object<Firesphere\Partia...\PartialFormSubmission>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
        $partialFormID = $partialForm->write();
75
        $partialForm->publishRecursive();
0 ignored issues
show
Documentation Bug introduced by
The method publishRecursive does not exist on object<Firesphere\Partia...\PartialFormSubmission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<Firesphere\Partia...\PartialFormSubmission>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
81
        return parent::setUp();
82
    }
83
84
    protected function tearDown()
85
    {
86
        parent::tearDown();
87
    }
88
}
89