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

PartialUserFormVerifyControllerTest::testInit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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\Controllers\PartialSubmissionController;
6
use Firesphere\PartialUserforms\Controllers\PartialUserFormVerifyController;
7
use Firesphere\PartialUserforms\Forms\PasswordForm;
8
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
9
use SilverStripe\Control\NullHTTPRequest;
10
use SilverStripe\Control\Session;
11
use SilverStripe\Dev\Debug;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\Form;
14
use SilverStripe\UserForms\Model\UserDefinedForm;
15
16
class PartialUserFormVerifyControllerTest extends SapphireTest
17
{
18
    protected static $fixture_file = '../fixtures/partialformtest.yml';
19
20
    /**
21
     * @expectedException  \SilverStripe\Control\HTTPResponse_Exception
22
     */
23
    public function testInit()
24
    {
25
        $request = new NullHTTPRequest();
26
        $session = new Session(['hi' => 'bye']);
27
        $request->setSession($session);
28
        $controller = PartialUserFormVerifyController::create();
29
        $controller->setRequest($request);
30
31
        $controller->init();
32
    }
33
34
    public function testInitSuccess()
35
    {
36
        $partialForm = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
37
        $request = new NullHTTPRequest();
38
        $session = new Session(['hi' => 'bye', PartialSubmissionController::SESSION_KEY => $partialForm->ID]);
39
        $request->setSession($session);
40
        $controller = PartialUserFormVerifyController::create();
41
        $controller->setRequest($request);
42
43
        $controller->init();
44
        $this->assertEquals(200, $controller->getResponse()->getStatusCode());
45
    }
46
47
    public function testDoValidate()
48
    {
49
        $request = new NullHTTPRequest();
50
        /** @var PartialFormSubmission $partialForm */
51
        $partialForm = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
52
        $parent = $partialForm->getParent();
53
        $parent->PasswordProtected = true;
54
        $parent->write();
0 ignored issues
show
Bug introduced by
The method write does only exist in SilverStripe\ORM\DataObject, but not in SilverStripe\UserForms\Model\UserDefinedForm.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
        $parent->publishRecursive();
56
        $partialForm->ParentID = $parent->ID;
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...
57
        $partialForm->write();
58
        $session = new Session(['hi' => 'bye', PartialSubmissionController::SESSION_KEY => $partialForm->ID]);
59
        $request->setSession($session);
60
        $controller = PartialUserFormVerifyController::create();
61
        $controller->setRequest($request);
62
63
        $this->assertNotNull($partialForm->Password);
64
65
        $testPwd = hash_pbkdf2('SHA256', '1234567890', $partialForm->TokenSalt, 1000);
66
67
        $partialForm->Password = $testPwd;
68
        $partialForm->write();
69
70
        $controller->init();
71
        $form = new PasswordForm($controller, __FUNCTION__);
72
        $result = $controller->doValidate(['Password' => '1234567890'], $form);
73
74
        $this->assertEquals($partialForm->ID, $session->get('PartialFormSession'));
75
        $this->assertContains('/partial/', $result->getHeader('Location'));
76
    }
77
}
78