Completed
Push — master ( 2fcfce...de1d77 )
by Robbie
11s
created

ShareDraftContentSiteTreeExtensionTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A testShareTokenLink() 0 71 1
1
<?php
2
3
namespace SilverStripe\ShareDraftContent\Tests;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Dev\FunctionalTest;
8
use SilverStripe\ShareDraftContent\Controllers\ShareDraftController;
9
use SilverStripe\ShareDraftContent\Extensions\ShareDraftContentSiteTreeExtension;
10
11
/**
12
 * @mixin PHPUnit_Framework_TestCase
13
 *
14
 * @package shareddraftcontent
15
 */
16
class ShareDraftContentSiteTreeExtensionTest extends FunctionalTest
17
{
18
    /**
19
     * @var string
20
     */
21
    protected static $fixture_file = 'ShareDraftContentSiteTreeExtensionTest.yml';
22
23
    public function testShareTokenLink()
24
    {
25
        /**
26
         * First we check if both pages generate new ShareTokenSalt values. Then we check that
27
         * these values are not the same.
28
         */
29
30
        Page::add_extension(ShareDraftContentSiteTreeExtension::class);
31
32
        /**
33
         * @var Page $firstSharedPage
34
         */
35
        $firstSharedPage = $this->objFromFixture(Page::class, 'FirstSharedPage');
36
37
        $firstShareLink = $firstSharedPage->ShareTokenLink();
38
39
        $this->assertNotEmpty($firstSharedPage->ShareTokenSalt);
40
41
        /**
42
         * @var page $secondSharedPage
43
         */
44
        $secondSharedPage = $this->objFromFixture(Page::class, 'SecondSharedPage');
45
46
        $secondShareLink = $secondSharedPage->ShareTokenLink();
47
48
        $this->assertNotEmpty($secondSharedPage->ShareTokenSalt);
49
50
        $this->assertNotEquals($firstShareLink, $secondShareLink);
51
52
        /**
53
         * Then we get the underlying token and send a preview request. With a valid key and token,
54
         * this will return a draft page. With an invalid key or token, this will return an expired
55
         * link page.
56
         */
57
58
        $firstSharedPageToken = $firstSharedPage->ShareTokens()->first();
59
60
        $this->assertNotEmpty($firstSharedPageToken);
61
62
        $parts = explode('/', $firstShareLink);
63
64
        $token = array_pop($parts);
65
        $key = array_pop($parts);
66
67
        $this->assertEquals($token, $firstSharedPageToken->Token);
68
69
        $request = new HTTPRequest('GET', $firstShareLink);
70
71
        $request->setRouteParams(array(
72
            'Token' => $token,
73
            'Key' => $key,
74
        ));
75
76
        $controller = new ShareDraftController($firstSharedPage);
0 ignored issues
show
Unused Code introduced by
The call to SilverStripe\ShareDraftC...ntroller::__construct() has too many arguments starting with $firstSharedPage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $controller = /** @scrutinizer ignore-call */ new ShareDraftController($firstSharedPage);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
77
78
        $response = $controller->preview($request);
79
80
        $this->assertContains('share-draft-content-message', $response);
81
82
        $request = new HTTPRequest('GET', $firstShareLink);
83
84
        $request->setRouteParams(array(
85
            'Token' => $token,
86
            'Key' => '',
87
        ));
88
89
        $controller = new ShareDraftController($firstSharedPage);
90
91
        $response = $controller->preview($request);
92
93
        $this->assertContains('share-draft-error-page', $response);
94
    }
95
}
96