PagesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Codeception\Util\Stub;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Page\Pages;
  6. use Grav\Common\Page\Page;
  7. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  8. /**
  9. * Class PagesTest
  10. */
  11. class PagesTest extends \Codeception\TestCase\Test
  12. {
  13. /** @var Grav $grav */
  14. protected $grav;
  15. /** @var Pages $pages */
  16. protected $pages;
  17. /** @var Page $root_page */
  18. protected $root_page;
  19. protected function _before()
  20. {
  21. $grav = Fixtures::get('grav');
  22. $this->grav = $grav();
  23. $this->pages = $this->grav['pages'];
  24. $this->grav['config']->set('system.home.alias', '/home');
  25. /** @var UniformResourceLocator $locator */
  26. $locator = $this->grav['locator'];
  27. $locator->addPath('page', '', 'tests/fake/simple-site/user/pages', false);
  28. $this->pages->init();
  29. }
  30. public function testBase()
  31. {
  32. $this->assertSame('', $this->pages->base());
  33. $this->pages->base('/test');
  34. $this->assertSame('/test', $this->pages->base());
  35. $this->pages->base('');
  36. $this->assertNull($this->pages->base());
  37. }
  38. public function testLastModified()
  39. {
  40. $this->assertNull($this->pages->lastModified());
  41. $this->pages->lastModified('test');
  42. $this->assertSame('test', $this->pages->lastModified());
  43. }
  44. public function testInstances()
  45. {
  46. $this->assertInternalType('array', $this->pages->instances());
  47. foreach($this->pages->instances() as $instance) {
  48. $this->assertInstanceOf('Grav\Common\Page\Page', $instance);
  49. }
  50. }
  51. public function testRoutes()
  52. {
  53. /** @var UniformResourceLocator $locator */
  54. $locator = $this->grav['locator'];
  55. $this->assertInternalType('array', $this->pages->routes());
  56. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/01.home', $this->pages->routes()['/']);
  57. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/01.home', $this->pages->routes()['/home']);
  58. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog', $this->pages->routes()['/blog']);
  59. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', $this->pages->routes()['/blog/post-one']);
  60. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', $this->pages->routes()['/blog/post-two']);
  61. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/03.about', $this->pages->routes()['/about']);
  62. }
  63. public function testAddPage()
  64. {
  65. /** @var UniformResourceLocator $locator */
  66. $locator = $this->grav['locator'];
  67. $path = $locator->findResource('tests://') . '/fake/single-pages/01.simple-page/default.md';
  68. $aPage = new Page();
  69. $aPage->init(new \SplFileInfo($path));
  70. $this->pages->addPage($aPage, '/new-page');
  71. $this->assertContains('/new-page', array_keys($this->pages->routes()));
  72. $this->assertSame($locator->findResource('tests://') . '/fake/single-pages/01.simple-page', $this->pages->routes()['/new-page']);
  73. }
  74. public function testSort()
  75. {
  76. /** @var UniformResourceLocator $locator */
  77. $locator = $this->grav['locator'];
  78. $aPage = $this->pages->dispatch('/blog');
  79. $subPagesSorted = $this->pages->sort($aPage);
  80. $this->assertInternalType('array', $subPagesSorted);
  81. $this->assertCount(2, $subPagesSorted);
  82. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[0]);
  83. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[1]);
  84. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  85. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  86. $this->assertSame(["slug" => "post-one"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one']);
  87. $this->assertSame(["slug" => "post-two"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two']);
  88. $subPagesSorted = $this->pages->sort($aPage, null, 'desc');
  89. $this->assertInternalType('array', $subPagesSorted);
  90. $this->assertCount(2, $subPagesSorted);
  91. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[0]);
  92. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[1]);
  93. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  94. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  95. $this->assertSame(["slug" => "post-one"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one']);
  96. $this->assertSame(["slug" => "post-two"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two']);
  97. }
  98. public function testSortCollection()
  99. {
  100. /** @var UniformResourceLocator $locator */
  101. $locator = $this->grav['locator'];
  102. $aPage = $this->pages->dispatch('/blog');
  103. $subPagesSorted = $this->pages->sortCollection($aPage->children(), $aPage->orderBy());
  104. $this->assertInternalType('array', $subPagesSorted);
  105. $this->assertCount(2, $subPagesSorted);
  106. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[0]);
  107. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[1]);
  108. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  109. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  110. $this->assertSame(["slug" => "post-one"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one']);
  111. $this->assertSame(["slug" => "post-two"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two']);
  112. $subPagesSorted = $this->pages->sortCollection($aPage->children(), $aPage->orderBy(), 'desc');
  113. $this->assertInternalType('array', $subPagesSorted);
  114. $this->assertCount(2, $subPagesSorted);
  115. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[0]);
  116. $this->assertSame($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[1]);
  117. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  118. $this->assertContains($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  119. $this->assertSame(["slug" => "post-one"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-one']);
  120. $this->assertSame(["slug" => "post-two"], $subPagesSorted[$locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog/post-two']);
  121. }
  122. public function testGet()
  123. {
  124. /** @var UniformResourceLocator $locator */
  125. $locator = $this->grav['locator'];
  126. //Page existing
  127. $aPage = $this->pages->get($locator->findResource('tests://') . '/fake/simple-site/user/pages/03.about');
  128. $this->assertInternalType('object', $aPage);
  129. $this->assertInstanceOf('Grav\Common\Page\Page', $aPage);
  130. //Page not existing
  131. $anotherPage = $this->pages->get($locator->findResource('tests://') . '/fake/simple-site/user/pages/03.non-existing');
  132. $this->assertNotInternalType('object', $anotherPage);
  133. $this->assertNull($anotherPage);
  134. }
  135. public function testChildren()
  136. {
  137. /** @var UniformResourceLocator $locator */
  138. $locator = $this->grav['locator'];
  139. //Page existing
  140. $children = $this->pages->children($locator->findResource('tests://') . '/fake/simple-site/user/pages/02.blog');
  141. $this->assertInstanceOf('Grav\Common\Page\Collection', $children);
  142. //Page not existing
  143. $children = $this->pages->children($locator->findResource('tests://') . '/fake/whatever/non-existing');
  144. $this->assertSame([], $children->toArray());
  145. }
  146. public function testDispatch()
  147. {
  148. $aPage = $this->pages->dispatch('/blog');
  149. $this->assertInstanceOf('Grav\Common\Page\Page', $aPage);
  150. $aPage = $this->pages->dispatch('/about');
  151. $this->assertInstanceOf('Grav\Common\Page\Page', $aPage);
  152. $aPage = $this->pages->dispatch('/blog/post-one');
  153. $this->assertInstanceOf('Grav\Common\Page\Page', $aPage);
  154. //Page not existing
  155. $aPage = $this->pages->dispatch('/non-existing');
  156. $this->assertNull($aPage);
  157. }
  158. public function testRoot()
  159. {
  160. $root = $this->pages->root();
  161. $this->assertInstanceOf('Grav\Common\Page\Page', $root);
  162. $this->assertSame('pages', $root->folder());
  163. }
  164. public function testBlueprints()
  165. {
  166. }
  167. public function testAll()
  168. {
  169. $this->assertInternalType('object', $this->pages->all());
  170. $this->assertInternalType('array', $this->pages->all()->toArray());
  171. foreach($this->pages->all() as $page) {
  172. $this->assertInstanceOf('Grav\Common\Page\Page', $page);
  173. }
  174. }
  175. public function testGetList()
  176. {
  177. $list = $this->pages->getList();
  178. $this->assertInternalType('array', $list);
  179. $this->assertSame('&mdash;-&rtrif; Home', $list['/']);
  180. $this->assertSame('&mdash;-&rtrif; Blog', $list['/blog']);
  181. }
  182. public function testGetTypes()
  183. {
  184. }
  185. public function testTypes()
  186. {
  187. }
  188. public function testModularTypes()
  189. {
  190. }
  191. public function testPageTypes()
  192. {
  193. }
  194. public function testAccessLevels()
  195. {
  196. }
  197. public function testParents()
  198. {
  199. }
  200. public function testParentsRawRoutes()
  201. {
  202. }
  203. public function testGetHomeRoute()
  204. {
  205. }
  206. public function testResetPages()
  207. {
  208. }
  209. }