getFormAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\ContentReview\Tests;
4
5
use Page;
6
use SilverStripe\CMS\Controllers\CMSPageEditController;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\ContentReview\Extensions\SiteTreeContentReview;
9
use SilverStripe\ContentReview\Extensions\ContentReviewOwner;
10
use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension;
11
use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings;
12
use SilverStripe\Control\HTTPRequest;
13
use SilverStripe\Control\HTTPResponse_Exception;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\Form;
16
use SilverStripe\Security\Group;
17
use SilverStripe\Security\Member;
18
use SilverStripe\SiteConfig\SiteConfig;
19
20
/**
21
 * @mixin PHPUnit_Framework_TestCase
22
 */
23
class ContentReviewCMSPageEditControllerTest extends ContentReviewBaseTest
24
{
25
    /**
26
     * @var string
27
     */
28
    protected static $fixture_file = 'ContentReviewTest.yml';
29
30
    /**
31
     * @var array
32
     */
33
    protected static $required_extensions = [
34
        SiteTree::class              => [SiteTreeContentReview::class],
35
        Group::class                 => [ContentReviewOwner::class],
36
        Member::class                => [ContentReviewOwner::class],
37
        CMSPageEditController::class => [ContentReviewCMSExtension::class],
38
        SiteConfig::class            => [ContentReviewDefaultSettings::class],
39
    ];
40
41 View Code Duplication
    public function testReviewedThrowsExceptionWithNoRecordID()
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...
42
    {
43
        $this->setExpectedException(HTTPResponse_Exception::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

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...
44
45
        /** @var CMSPageEditController|ContentReviewCMSExtension $controller */
46
        $controller = new CMSPageEditController();
47
48
        $dummyForm = new Form($controller, "EditForm", new FieldList(), new FieldList());
49
50
        $controller->savereview(array(
0 ignored issues
show
Bug introduced by
The method savereview does only exist in SilverStripe\ContentRevi...ntentReviewCMSExtension, but not in SilverStripe\CMS\Controllers\CMSPageEditController.

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...
51
            "ID"      => null,
52
            "Message" => null,
53
        ), $dummyForm);
54
    }
55
56 View Code Duplication
    public function testReviewedThrowsExceptionWithWrongRecordID()
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
        $this->setExpectedException(HTTPResponse_Exception::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

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
60
        /** @var CMSPageEditController|ContentReviewCMSExtension $controller */
61
        $controller = new CMSPageEditController();
62
63
        $dummyForm = new Form($controller, "EditForm", new FieldList(), new FieldList());
64
65
        $controller->savereview(array(
0 ignored issues
show
Bug introduced by
The method savereview does only exist in SilverStripe\ContentRevi...ntentReviewCMSExtension, but not in SilverStripe\CMS\Controllers\CMSPageEditController.

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...
66
            "ID"      => "FAIL",
67
            "Message" => null,
68
        ), $dummyForm);
69
    }
70
71
    public function testReviewedWithAuthor()
72
    {
73
        /** @var Member $author */
74
        $author = $this->objFromFixture(Member::class, "author");
75
76
        $this->logInAs($author);
77
78
        /** @var Page|SiteTreeContentReview $page */
79
        $page = $this->objFromFixture(Page::class, "home");
80
81
        $data = array(
82
            "action_savereview" => 1,
83
            "ID" => $page->ID,
84
        );
85
86
        $this->get('admin/pages/edit/show/' . $page->ID);
87
        $response = $this->post($this->getFormAction($page), $data);
0 ignored issues
show
Bug introduced by
It seems like $page defined by $this->objFromFixture(\Page::class, 'home') on line 79 can also be of type object<SilverStripe\Cont...\SiteTreeContentReview>; however, SilverStripe\ContentRevi...erTest::getFormAction() does only seem to accept object<Page>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
89
        $this->assertEquals("OK", $response->getStatusDescription());
90
        $this->assertEquals(200, $response->getStatusCode());
91
    }
92
93
    /**
94
     * Return a CMS page edit form action via using a dummy request and session
95
     *
96
     * @param Page $page
97
     * @return string
98
     */
99
    protected function getFormAction(Page $page)
100
    {
101
        $controller = singleton(CMSPageEditController::class);
102
        $controller->setRequest(new HTTPRequest('GET', '/'));
103
        $controller->getRequest()->setSession($this->session());
104
105
        return $controller->getEditForm($page->ID)->FormAction();
106
    }
107
}
108