PagesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Page\Pages;
  5. use Grav\Common\Page\Page;
  6. use Grav\Common\Page\Interfaces\PageInterface;
  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 PageInterface $root_page */
  18. protected $root_page;
  19. protected function _before(): void
  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(): void
  31. {
  32. self::assertSame('', $this->pages->base());
  33. $this->pages->base('/test');
  34. self::assertSame('/test', $this->pages->base());
  35. $this->pages->base('');
  36. self::assertSame($this->pages->base(), '');
  37. }
  38. public function testLastModified(): void
  39. {
  40. self::assertNull($this->pages->lastModified());
  41. $this->pages->lastModified('test');
  42. self::assertSame('test', $this->pages->lastModified());
  43. }
  44. public function testInstances(): void
  45. {
  46. self::assertIsArray($this->pages->instances());
  47. foreach ($this->pages->instances() as $instance) {
  48. self::assertInstanceOf(PageInterface::class, $instance);
  49. }
  50. }
  51. public function testRoutes(): void
  52. {
  53. /** @var UniformResourceLocator $locator */
  54. $locator = $this->grav['locator'];
  55. $folder = $locator->findResource('tests://');
  56. self::assertIsArray($this->pages->routes());
  57. self::assertSame($folder . '/fake/simple-site/user/pages/01.home', $this->pages->routes()['/']);
  58. self::assertSame($folder . '/fake/simple-site/user/pages/01.home', $this->pages->routes()['/home']);
  59. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog', $this->pages->routes()['/blog']);
  60. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-one', $this->pages->routes()['/blog/post-one']);
  61. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-two', $this->pages->routes()['/blog/post-two']);
  62. self::assertSame($folder . '/fake/simple-site/user/pages/03.about', $this->pages->routes()['/about']);
  63. }
  64. public function testAddPage(): void
  65. {
  66. /** @var UniformResourceLocator $locator */
  67. $locator = $this->grav['locator'];
  68. $folder = $locator->findResource('tests://');
  69. $path = $folder . '/fake/single-pages/01.simple-page/default.md';
  70. $aPage = new Page();
  71. $aPage->init(new \SplFileInfo($path));
  72. $this->pages->addPage($aPage, '/new-page');
  73. self::assertContains('/new-page', array_keys($this->pages->routes()));
  74. self::assertSame($folder . '/fake/single-pages/01.simple-page', $this->pages->routes()['/new-page']);
  75. }
  76. public function testSort(): void
  77. {
  78. /** @var UniformResourceLocator $locator */
  79. $locator = $this->grav['locator'];
  80. $folder = $locator->findResource('tests://');
  81. $aPage = $this->pages->find('/blog');
  82. $subPagesSorted = $this->pages->sort($aPage);
  83. self::assertIsArray($subPagesSorted);
  84. self::assertCount(2, $subPagesSorted);
  85. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[0]);
  86. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[1]);
  87. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  88. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  89. self::assertSame(['slug' => 'post-one'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-one']);
  90. self::assertSame(['slug' => 'post-two'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-two']);
  91. $subPagesSorted = $this->pages->sort($aPage, null, 'desc');
  92. self::assertIsArray($subPagesSorted);
  93. self::assertCount(2, $subPagesSorted);
  94. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[0]);
  95. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[1]);
  96. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  97. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  98. self::assertSame(['slug' => 'post-one'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-one']);
  99. self::assertSame(['slug' => 'post-two'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-two']);
  100. }
  101. public function testSortCollection(): void
  102. {
  103. /** @var UniformResourceLocator $locator */
  104. $locator = $this->grav['locator'];
  105. $folder = $locator->findResource('tests://');
  106. $aPage = $this->pages->find('/blog');
  107. $subPagesSorted = $this->pages->sortCollection($aPage->children(), $aPage->orderBy());
  108. self::assertIsArray($subPagesSorted);
  109. self::assertCount(2, $subPagesSorted);
  110. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[0]);
  111. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[1]);
  112. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  113. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  114. self::assertSame(['slug' => 'post-one'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-one']);
  115. self::assertSame(['slug' => 'post-two'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-two']);
  116. $subPagesSorted = $this->pages->sortCollection($aPage->children(), $aPage->orderBy(), 'desc');
  117. self::assertIsArray($subPagesSorted);
  118. self::assertCount(2, $subPagesSorted);
  119. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted)[0]);
  120. self::assertSame($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted)[1]);
  121. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-one', array_keys($subPagesSorted));
  122. self::assertContains($folder . '/fake/simple-site/user/pages/02.blog/post-two', array_keys($subPagesSorted));
  123. self::assertSame(['slug' => 'post-one'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-one']);
  124. self::assertSame(['slug' => 'post-two'], $subPagesSorted[$folder . '/fake/simple-site/user/pages/02.blog/post-two']);
  125. }
  126. public function testGet(): void
  127. {
  128. /** @var UniformResourceLocator $locator */
  129. $locator = $this->grav['locator'];
  130. $folder = $locator->findResource('tests://');
  131. //Page existing
  132. $aPage = $this->pages->get($folder . '/fake/simple-site/user/pages/03.about');
  133. self::assertInstanceOf(PageInterface::class, $aPage);
  134. //Page not existing
  135. $anotherPage = $this->pages->get($folder . '/fake/simple-site/user/pages/03.non-existing');
  136. self::assertNull($anotherPage);
  137. }
  138. public function testChildren(): void
  139. {
  140. /** @var UniformResourceLocator $locator */
  141. $locator = $this->grav['locator'];
  142. $folder = $locator->findResource('tests://');
  143. //Page existing
  144. $children = $this->pages->children($folder . '/fake/simple-site/user/pages/02.blog');
  145. self::assertInstanceOf('Grav\Common\Page\Collection', $children);
  146. //Page not existing
  147. $children = $this->pages->children($folder . '/fake/whatever/non-existing');
  148. self::assertSame([], $children->toArray());
  149. }
  150. public function testDispatch(): void
  151. {
  152. $aPage = $this->pages->dispatch('/blog');
  153. self::assertInstanceOf(PageInterface::class, $aPage);
  154. $aPage = $this->pages->dispatch('/about');
  155. self::assertInstanceOf(PageInterface::class, $aPage);
  156. $aPage = $this->pages->dispatch('/blog/post-one');
  157. self::assertInstanceOf(PageInterface::class, $aPage);
  158. //Page not existing
  159. $aPage = $this->pages->dispatch('/non-existing');
  160. self::assertNull($aPage);
  161. }
  162. public function testRoot(): void
  163. {
  164. $root = $this->pages->root();
  165. self::assertInstanceOf(PageInterface::class, $root);
  166. self::assertSame('pages', $root->folder());
  167. }
  168. public function testBlueprints(): void
  169. {
  170. }
  171. public function testAll()
  172. {
  173. self::assertIsObject($this->pages->all());
  174. self::assertIsArray($this->pages->all()->toArray());
  175. foreach ($this->pages->all() as $page) {
  176. self::assertInstanceOf(PageInterface::class, $page);
  177. }
  178. }
  179. public function testGetList(): void
  180. {
  181. $list = $this->pages->getList();
  182. self::assertIsArray($list);
  183. self::assertSame('&mdash;-&rtrif; Home', $list['/']);
  184. self::assertSame('&mdash;-&rtrif; Blog', $list['/blog']);
  185. }
  186. public function testTranslatedLanguages(): void
  187. {
  188. /** @var UniformResourceLocator $locator */
  189. $locator = $this->grav['locator'];
  190. $folder = $locator->findResource('tests://');
  191. $page = $this->pages->get($folder . '/fake/simple-site/user/pages/04.page-translated');
  192. $this->assertInstanceOf(PageInterface::class, $page);
  193. $translatedLanguages = $page->translatedLanguages();
  194. $this->assertIsArray($translatedLanguages);
  195. $this->assertSame(["en" => "/page-translated", "fr" => "/page-translated"], $translatedLanguages);
  196. }
  197. public function testLongPathTranslatedLanguages(): void
  198. {
  199. /** @var UniformResourceLocator $locator */
  200. $locator = $this->grav['locator'];
  201. $folder = $locator->findResource('tests://');
  202. $page = $this->pages->get($folder . '/fake/simple-site/user/pages/05.translatedlong/part2');
  203. $this->assertInstanceOf(PageInterface::class, $page);
  204. $translatedLanguages = $page->translatedLanguages();
  205. $this->assertIsArray($translatedLanguages);
  206. $this->assertSame(["en" => "/translatedlong/part2", "fr" => "/translatedlong/part2"], $translatedLanguages);
  207. }
  208. public function testGetTypes(): void
  209. {
  210. }
  211. public function testTypes(): void
  212. {
  213. }
  214. public function testModularTypes(): void
  215. {
  216. }
  217. public function testPageTypes(): void
  218. {
  219. }
  220. public function testAccessLevels(): void
  221. {
  222. }
  223. public function testParents(): void
  224. {
  225. }
  226. public function testParentsRawRoutes(): void
  227. {
  228. }
  229. public function testGetHomeRoute(): void
  230. {
  231. }
  232. public function testResetPages(): void
  233. {
  234. }
  235. }