Completed
Push — master ( c07b10...3eba7c )
by Simon
13s
created

PartialFormSubmissionTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 141
Duplicated Lines 25.53 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 36
loc 141
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCMSFields() 0 11 1
A testCanCreate() 9 9 1
A testCanView() 9 9 1
A testCanEdit() 9 9 1
A testCanDelete() 9 9 1
A testGetParent() 0 6 1
A testGetPartialLink() 0 20 1
A testGetPartialToken() 0 16 1
A testGenerateToken() 0 7 1
A testGenerateKey() 0 11 1
A setUp() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Firesphere\PartialUserforms\Tests;
4
5
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\UserForms\Model\UserDefinedForm;
12
13
class PartialFormSubmissionTest extends SapphireTest
14
{
15
    /**
16
     * @var bool
17
     */
18
    protected $usesDatabase = true;
19
20
    /**
21
     * @var PartialFormSubmission
22
     */
23
    protected $submission;
24
25
    public function testGetCMSFields()
26
    {
27
        $fields = $this->submission->getCMSFields();
28
29
        $this->assertNull($fields->dataFieldByName('Values'));
30
        $this->assertNull($fields->dataFieldByName('IsSend'));
31
        $this->assertNull($fields->dataFieldByName('TokenSalt'));
32
        $this->assertNull($fields->dataFieldByName('Token'));
33
        $this->assertNull($fields->dataFieldByName('UserDefinedFormID'));
34
        $this->assertInstanceOf(GridField::class, $fields->dataFieldByName('PartialFields'));
35
    }
36
37 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...
38
    {
39
        $this->assertTrue($this->submission->canCreate());
40
41
        $this->submission->ParentID = $this->submission->UserDefinedFormID;
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...
42
        $this->submission->UserDefinedFormID = 0;
43
44
        $this->assertTrue($this->submission->canCreate());
45
    }
46
47 View Code Duplication
    public function testCanView()
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...
48
    {
49
        $this->assertTrue($this->submission->canView());
50
51
        $this->submission->ParentID = $this->submission->UserDefinedFormID;
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...
52
        $this->submission->UserDefinedFormID = 0;
53
54
        $this->assertTrue($this->submission->canView());
55
    }
56
57 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...
58
    {
59
        $this->assertTrue($this->submission->canEdit());
60
61
        $this->submission->ParentID = $this->submission->UserDefinedFormID;
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...
62
        $this->submission->UserDefinedFormID = 0;
63
64
        $this->assertTrue($this->submission->canEdit());
65
    }
66
67 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...
68
    {
69
        $this->assertTrue($this->submission->canDelete());
70
71
        $this->submission->ParentID = $this->submission->UserDefinedFormID;
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...
72
        $this->submission->UserDefinedFormID = 0;
73
74
        $this->assertTrue($this->submission->canDelete());
75
    }
76
77
    public function testGetParent()
78
    {
79
        $parent = $this->submission->getParent();
80
81
        $this->assertInstanceOf(UserDefinedForm::class, $parent);
82
    }
83
84
    public function testGetPartialLink()
85
    {
86
        $partial = PartialFormSubmission::create();
87
        $this->assertEquals('(none)', $partial->getPartialLink());
88
89
        $partial->write();
90
        $this->assertNotEquals('(none)', $partial->getPartialLink());
91
92
        $partial->Token = 'test-token';
0 ignored issues
show
Documentation introduced by
The property Token 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...
93
        $partial->TokenSalt = 'test-salt';
0 ignored issues
show
Documentation introduced by
The property TokenSalt 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...
94
        $partial->write();
95
96
        $link = Controller::join_links(
97
            Director::absoluteBaseURL(),
98
            'partial',
99
            $partial->generateKey('test-token'),
100
            'test-token'
101
        );
102
        $this->assertEquals($link, $partial->getPartialLink());
103
    }
104
105
    public function testGetPartialToken()
106
    {
107
        $partial = PartialFormSubmission::create();
108
        $partial->TokenSalt = 'test-salt';
0 ignored issues
show
Documentation introduced by
The property TokenSalt 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...
109
        $this->assertNull(TestHelper::invokeMethod($partial, 'getPartialToken'));
110
111
        $partial->TokenSalt = null;
112
        $partial->write();
113
        $this->assertNotNull(TestHelper::invokeMethod($partial, 'getPartialToken'));
114
115
        $partial->Token = 'test-token';
116
        $partial->TokenSalt = 'test-salt';
117
        $partial->write();
118
119
        $this->assertEquals('test-token', TestHelper::invokeMethod($partial, 'getPartialToken'));
120
    }
121
122
    public function testGenerateToken()
123
    {
124
        $partial = singleton(PartialFormSubmission::class);
125
        $token = TestHelper::invokeMethod($partial, 'generateToken');
126
        $this->assertNotNull($token);
127
        $this->assertEquals(16, strlen($token));
128
    }
129
130
    public function testGenerateKey()
131
    {
132
        $partial = PartialFormSubmission::create();
133
        $token = 'test-token';
134
        $this->assertEquals('b041b97441e59b3a', $partial->generateKey($token));
135
136
        $partial->TokenSalt = 'test-salt';
0 ignored issues
show
Documentation introduced by
The property TokenSalt 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...
137
        $key = $partial->generateKey($token);
138
        $this->assertEquals('30505a53806d5de9', $key);
139
        $this->assertEquals(16, strlen($key));
140
    }
141
142
    protected function setUp()
143
    {
144
        $this->submission = Injector::inst()->get(PartialFormSubmission::class);
145
        $form = UserDefinedForm::create(['Title' => 'Test']);
146
        $formID = $form->write();
147
        $this->submission->UserDefinedFormID = $formID;
148
        $this->submission->UserDefinedFormClass = UserDefinedForm::class;
149
        $this->submission->write();
150
151
        return parent::setUp();
152
    }
153
}
154