BannerBlockTest::testCallToActionLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\Tests\Block;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\ElementalBlocks\Block\BannerBlock;
8
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
9
use SilverStripe\View\ArrayData;
10
use SilverStripe\View\Requirements;
11
12
class BannerBlockTest extends SapphireTest
13
{
14
    protected static $fixture_file = 'BannerBlockTest.yml';
15
16
    public function testTinyMceJavascriptIsRequiredBeforeBlocks()
17
    {
18
        $block = new BannerBlock;
19
        $block->getCMSFields();
20
21
        $javascript = Requirements::backend()->getJavascript();
22
23
        // Ensure TinyMCE's scripts are loaded first
24
        $mcePath = TinyMCEConfig::get()->getScriptURL();
25
        $this->assertArrayHasKey($mcePath, $javascript, 'TinyMCE is loaded first');
26
27
        // By pushing the bundle reference again, the size of the requirements shouldn't change
28
        $this->assertNotEmpty($javascript);
29
        Requirements::javascript('silverstripe/elemental-blocks:client/dist/js/bundle.js');
30
        $this->assertSame(
31
            count($javascript),
32
            count(Requirements::backend()->getJavascript()),
33
            'Blocks bundle is added'
34
        );
35
    }
36
37
    public function testCallToActionLink()
38
    {
39
        $block = new BannerBlock;
40
        $this->assertNull($block->CallToActionLink(), 'No link data set returns null');
41
42
        $block->CallToActionLink = json_encode([
0 ignored issues
show
Bug Best Practice introduced by
The property CallToActionLink does not exist on SilverStripe\ElementalBlocks\Block\BannerBlock. Since you implemented __set, consider adding a @property annotation.
Loading history...
43
            'PageID' => $this->idFromFixture(SiteTree::class, 'test_page'),
44
            'Text' => 'Click here',
45
            'Description' => 'Link title text',
46
            'TargetBlank' => true,
47
        ]);
48
49
        $result = $block->CallToActionLink();
50
        $this->assertInstanceOf(ArrayData::class, $result, 'ArrayData object is returned');
51
        $this->assertEquals($this->idFromFixture(SiteTree::class, 'test_page'), $result->Page->ID, 'Page is attached');
52
        $this->assertInstanceOf(SiteTree::class, $result->Page);
53
        $this->assertSame('Link title text', $result->Description, 'Link attributes are available');
54
    }
55
}
56