-
Notifications
You must be signed in to change notification settings - Fork 7
/
CmsBlockTestCase.php
136 lines (125 loc) · 3.44 KB
/
CmsBlockTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace luya\testsuite\cases;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use yii\base\InvalidConfigException;
/**
* Cms module block TestCase.
*
* Testing a CMS Block for the CMS Layout. Example usage:
*
* ```php
* class TextBlockTest extends CmsBlockTestCase
* {
* public $blockClass = 'luya\generic\blocks\TextBlock';
*
* public function getConfigArray()
* {
* return [
* 'id' => 'testTextBlock',
* 'basePath' => dirname(__DIR__),
* ];
* }
*
* public function testAdminAndFrontendRender()
* {
* $this->assertSame('<p>Removes spaces and br from frontend view.</p>', $this->renderFrontendNoSpace());
* $this->assertSame('<p>Admin View</p>', $this->renderAdminNoSpace());
* }
*
* public function testFrontendWithVars()
* {
* $this->block->setVarValues([
* 'text' => 'Hello World',
* ]);
*
* $this->assertSame('<p>Hello World', $this->renderFrontendNoSpace());
* }
* }
* ```
*
* On order to test an image object from storage you could use
*
* ```php
* $this->block->setVarValues(['image' => (object) ['source' => 'image.jpg', 'caption' => 'image caption']]);
* ```
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.6
*/
abstract class CmsBlockTestCase extends WebApplicationTestCase
{
/**
* @var string The path to the block to be tested.
*/
public $blockClass;
/**
* @var \luya\cms\base\PhpBlock
*/
public $block;
/**
* @inheritdoc
*/
public function afterSetup()
{
parent::afterSetup();
if (!$this->blockClass) {
throw new InvalidConfigException("The 'blockClass' property can not be empty.");
}
$class = $this->blockClass;
$this->block = new $class();
}
/**
* Renders the frontend view of a block.
*
* @return string
*/
public function renderFrontend()
{
$icon = $this->block->icon();
if (empty($icon)) {
$icon = 'fa';
}
$this->assertNotEmpty($this->block->blockGroup());
$this->assertNotEmpty($this->block->name());
$this->assertTrue(is_string($icon));
$this->assertTrue(is_array($this->block->config()));
$this->assertTrue(is_array($this->block->extraVars()));
$this->assertFalse(is_array($this->block->renderAdmin()));
$this->assertNotNull($this->block->getFieldHelp());
return $this->block->renderFrontend();
}
/**
* Renders the admin view of a blockl.
*
* @return string
*/
public function renderAdmin()
{
$twig = new Environment(new FilesystemLoader());
$temp = $twig->createTemplate($this->block->renderAdmin());
return $temp->render([
'cfgs' => $this->block->getCfgValues(),
'vars' => $this->block->getVarValues(),
'extras' => $this->block->getExtraVarValues(),
]);
}
/**
* Render the admin view, but auto remove all spaces.
*
* @return mixed
*/
public function renderAdminNoSpace()
{
return $this->trimContent($this->renderAdmin());
}
/**
* Render the frontend view, but auto remove all spaces.
*
* @return string
*/
public function renderFrontendNoSpace()
{
return $this->trimContent($this->renderFrontend());
}
}