PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/cms/classes/CmsCompoundObjectTest.php

https://gitlab.com/gideonmarked/wellmarketing
PHP | 259 lines | 185 code | 59 blank | 15 comment | 4 complexity | 88721ed4f6375326d826e2517f9f1711 MD5 | raw file
  1. <?php
  2. use Cms\Classes\Theme;
  3. use Cms\Classes\CmsObject;
  4. use Cms\Classes\CmsCompoundObject;
  5. class TestCmsCompoundObject extends CmsCompoundObject
  6. {
  7. protected function parseSettings() {}
  8. public static function getObjectTypeDirName()
  9. {
  10. return 'testobjects';
  11. }
  12. }
  13. class TestTemporaryCmsCompoundObject extends CmsCompoundObject
  14. {
  15. protected function parseSettings() {}
  16. public static function getObjectTypeDirName()
  17. {
  18. return 'temporary';
  19. }
  20. }
  21. class CmsCompoundObjectTest extends TestCase
  22. {
  23. public function testLoadFile()
  24. {
  25. $theme = Theme::load('test');
  26. $obj = TestCmsCompoundObject::load($theme, 'compound.htm');
  27. $this->assertContains("\$controller->data['something'] = 'some value'", $obj->code);
  28. $this->assertEquals('<p>This is a paragraph</p>', $obj->markup);
  29. $this->assertInternalType('array', $obj->settings);
  30. $this->assertArrayHasKey('var', $obj->settings);
  31. $this->assertEquals('value', $obj->settings['var']);
  32. $this->assertArrayHasKey('components', $obj->settings);
  33. $this->assertArrayHasKey('section', $obj->settings['components']);
  34. $this->assertInternalType('array', $obj->settings['components']['section']);
  35. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  36. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  37. $this->assertEquals('value', $obj->var);
  38. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  39. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  40. }
  41. public function testParseComponentSettings()
  42. {
  43. $theme = Theme::load('test');
  44. $obj = TestCmsCompoundObject::load($theme, 'component.htm');
  45. $this->assertArrayHasKey('components', $obj->settings);
  46. $this->assertInternalType('array', $obj->settings['components']);
  47. $this->assertArrayHasKey('testArchive', $obj->settings['components']);
  48. $this->assertArrayHasKey('posts-per-page', $obj->settings['components']['testArchive']);
  49. $this->assertEquals(10, $obj->settings['components']['testArchive']['posts-per-page']);
  50. }
  51. public function testHasComponent()
  52. {
  53. $theme = Theme::load('test');
  54. $obj = TestCmsCompoundObject::load($theme, 'components.htm');
  55. $this->assertArrayHasKey('components', $obj->settings);
  56. $this->assertInternalType('array', $obj->settings['components']);
  57. $this->assertArrayHasKey('testArchive firstAlias', $obj->settings['components']);
  58. $this->assertArrayHasKey('October\Tester\Components\Post secondAlias', $obj->settings['components']);
  59. // Explicit
  60. $this->assertEquals('testArchive firstAlias', $obj->hasComponent('testArchive'));
  61. $this->assertEquals('October\Tester\Components\Post secondAlias', $obj->hasComponent('October\Tester\Components\Post'));
  62. // Resolved
  63. $this->assertEquals('testArchive firstAlias', $obj->hasComponent('October\Tester\Components\Archive'));
  64. $this->assertEquals('October\Tester\Components\Post secondAlias', $obj->hasComponent('testPost'));
  65. // Negative test
  66. $this->assertFalse($obj->hasComponent('yooHooBigSummerBlowOut'));
  67. $this->assertFalse($obj->hasComponent('October\Tester\Components\BigSummer'));
  68. }
  69. public function testCache()
  70. {
  71. $theme = Theme::load('test');
  72. $themePath = $theme->getPath();
  73. /*
  74. * Prepare the test file
  75. */
  76. $srcPath = $themePath.'/testobjects/compound.htm';
  77. $this->assertFileExists($srcPath);
  78. $testContent = file_get_contents($srcPath);
  79. $this->assertNotEmpty($testContent);
  80. $filePath = $themePath .= '/temporary/testcompound.htm';
  81. if (file_exists($filePath))
  82. @unlink($filePath);
  83. $this->assertFileNotExists($filePath);
  84. file_put_contents($filePath, $testContent);
  85. /*
  86. * Load the test object to initialize the cache
  87. */
  88. $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm');
  89. $this->assertFalse($obj->isLoadedFromCache());
  90. $this->assertEquals($testContent, $obj->getContent());
  91. $this->assertEquals('testcompound.htm', $obj->getFileName());
  92. $this->assertEquals('<p>This is a paragraph</p>', $obj->markup);
  93. $this->assertInternalType('array', $obj->settings);
  94. $this->assertArrayHasKey('var', $obj->settings);
  95. $this->assertEquals('value', $obj->settings['var']);
  96. $this->assertArrayHasKey('components', $obj->settings);
  97. $this->assertInternalType('array', $obj->settings['components']['section']);
  98. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  99. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  100. $this->assertEquals('value', $obj->var);
  101. $this->assertInternalType('array', $obj->settings['components']['section']);
  102. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  103. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  104. /*
  105. * Load the test object again, it should be loaded from the cache this time
  106. */
  107. CmsObject::clearInternalCache();
  108. $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm');
  109. $this->assertTrue($obj->isLoadedFromCache());
  110. $this->assertEquals($testContent, $obj->getContent());
  111. $this->assertEquals('testcompound.htm', $obj->getFileName());
  112. $this->assertEquals('<p>This is a paragraph</p>', $obj->markup);
  113. $this->assertInternalType('array', $obj->settings);
  114. $this->assertArrayHasKey('var', $obj->settings);
  115. $this->assertEquals('value', $obj->settings['var']);
  116. $this->assertArrayHasKey('components', $obj->settings);
  117. $this->assertInternalType('array', $obj->settings['components']['section']);
  118. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  119. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  120. $this->assertEquals('value', $obj->var);
  121. $this->assertInternalType('array', $obj->settings['components']['section']);
  122. $this->assertArrayHasKey('version', $obj->settings['components']['section']);
  123. $this->assertEquals(10, $obj->settings['components']['section']['version']);
  124. }
  125. public function testUndefinedProperty()
  126. {
  127. $theme = Theme::load('test');
  128. $obj = new TestCmsCompoundObject($theme);
  129. $this->assertNull($obj->something);
  130. }
  131. public function testSaveMarkup()
  132. {
  133. $theme = Theme::load('apitest');
  134. $destFilePath = $theme->getPath().'/testobjects/compound-markup.htm';
  135. if (file_exists($destFilePath))
  136. unlink($destFilePath);
  137. $this->assertFileNotExists($destFilePath);
  138. $obj = new TestCmsCompoundObject($theme);
  139. $obj->fill([
  140. 'markup' => '<p>Hello, world!</p>',
  141. 'fileName'=>'compound-markup'
  142. ]);
  143. $obj->save();
  144. $referenceFilePath = base_path().'/tests/fixtures/cms/reference/compound-markup.htm';
  145. $this->assertFileExists($referenceFilePath);
  146. $this->assertFileExists($destFilePath);
  147. $this->assertFileEqualsNormalized($referenceFilePath, $destFilePath);
  148. }
  149. public function testSaveMarkupAndSettings()
  150. {
  151. $theme = Theme::load('apitest');
  152. $destFilePath = $theme->getPath().'/testobjects/compound-markup-settings.htm';
  153. if (file_exists($destFilePath))
  154. unlink($destFilePath);
  155. $this->assertFileNotExists($destFilePath);
  156. $obj = new TestCmsCompoundObject($theme);
  157. $obj->fill([
  158. 'settings'=>['var'=>'value'],
  159. 'markup' => '<p>Hello, world!</p>',
  160. 'fileName'=>'compound-markup-settings'
  161. ]);
  162. $obj->save();
  163. $referenceFilePath = base_path().'/tests/fixtures/cms/reference/compound-markup-settings.htm';
  164. $this->assertFileExists($referenceFilePath);
  165. $this->assertFileExists($destFilePath);
  166. $this->assertFileEqualsNormalized($referenceFilePath, $destFilePath);
  167. }
  168. public function testSaveFull()
  169. {
  170. $theme = Theme::load('apitest');
  171. $destFilePath = $theme->getPath().'/testobjects/compound.htm';
  172. if (file_exists($destFilePath)) {
  173. unlink($destFilePath);
  174. }
  175. $this->assertFileNotExists($destFilePath);
  176. $obj = new TestCmsCompoundObject($theme);
  177. $obj->fill([
  178. 'fileName'=>'compound',
  179. 'settings'=>['var'=>'value'],
  180. 'code' => 'function a() {return true;}',
  181. 'markup' => '<p>Hello, world!</p>'
  182. ]);
  183. $obj->save();
  184. $referenceFilePath = base_path().'/tests/fixtures/cms/reference/compound-full.htm';
  185. $this->assertFileExists($referenceFilePath);
  186. $this->assertFileExists($destFilePath);
  187. $this->assertFileEqualsNormalized($referenceFilePath, $destFilePath);
  188. }
  189. //
  190. // Helpers
  191. //
  192. protected function assertFileEqualsNormalized($expected, $actual)
  193. {
  194. $expected = file_get_contents($expected);
  195. $expected = preg_replace('~\R~u', PHP_EOL, $expected); // Normalize EOL
  196. $actual = file_get_contents($actual);
  197. $actual = preg_replace('~\R~u', PHP_EOL, $actual); // Normalize EOL
  198. $this->assertEquals($expected, $actual);
  199. }
  200. }