SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/navigationTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13. use Sprout\Helpers\Enc;
  14. use Sprout\Helpers\Navigation;
  15. use Sprout\Helpers\Pdb;
  16.  
  17.  
  18. class navigationTest extends PHPUnit_Framework_TestCase
  19. {
  20.  
  21. // pages.id, pages.parent_id, pages.active, pages.name, page_revisions.type, page_revisions.status
  22. private static $fake_pages = [
  23. [1, 0, 1, 'Top level one - std live', 'standard', 'live'],
  24. [2, 0, 1, 'Top level two - std live', 'standard', 'live'],
  25. [3, 0, 0, 'Top level three - inactive', 'standard', 'live'],
  26. [4, 0, 1, 'Top level four - unpublished', 'standard', 'wip'],
  27. ];
  28.  
  29. /**
  30.   * Duplicate pages tables and then inject some fake data
  31.   */
  32. public static function setUpBeforeClass()
  33. {
  34. $rand = time(0,9999);
  35.  
  36. $q = "CREATE TEMPORARY TABLE unit_test_{$rand}_pages SELECT * FROM ~pages WHERE 0";
  37. Pdb::q($q, [], 'null');
  38.  
  39. $q = "CREATE TEMPORARY TABLE unit_test_{$rand}_page_revisions SELECT * FROM ~page_revisions WHERE 0";
  40. Pdb::q($q, [], 'null');
  41.  
  42. foreach (self::$fake_pages as $pg) {
  43. $status = array_pop($pg);
  44. $type = array_pop($pg);
  45.  
  46. $pg[4] = Enc::urlname($pg[3]);
  47. $q = "INSERT INTO unit_test_{$rand}_pages SET id = ?, subsite_id = 1, parent_id = ?, active = ?, name = ?, slug = ?";
  48. Pdb::q($q, $pg, 'null');
  49.  
  50. if ($type and $status) {
  51. $q = "INSERT INTO unit_test_{$rand}_page_revisions SET page_id = ?, type = ?, status = ?";
  52. Pdb::q($q, [$pg[0], $type, $status], 'null');
  53. }
  54. }
  55.  
  56. Pdb::setTablePrefixOverride('pages', "unit_test_{$rand}_");
  57. Pdb::setTablePrefixOverride('page_revisions', "unit_test_{$rand}_");
  58. }
  59.  
  60. /**
  61.   * Quick and dirty check that the INSERTs above actually worked
  62.   */
  63. public function testDatabaseSetupWorked()
  64. {
  65. $q = "SELECT * FROM ~pages WHERE parent_id = 0";
  66. $top_pages = Pdb::query($q, [], 'arr');
  67. $this->assertCount(4, $top_pages);
  68. $this->assertEquals('Top level one - std live', $top_pages[0]['name']);
  69. $this->assertEquals('Top level two - std live', $top_pages[1]['name']);
  70. $this->assertEquals('Top level three - inactive', $top_pages[2]['name']);
  71. $this->assertEquals('Top level four - unpublished', $top_pages[3]['name']);
  72. }
  73.  
  74. /**
  75.   * Put the prefixes back
  76.   */
  77. public static function tearDownAfterClass()
  78. {
  79. Pdb::setTablePrefixOverride('pages', Pdb::prefix());
  80. Pdb::setTablePrefixOverride('page_revisions', Pdb::prefix());
  81. }
  82.  
  83.  
  84. /**
  85.   * Admin loading should bring in all pages, including inactive and unpublished
  86.   */
  87. public function testLoadPageTree__Admin()
  88. {
  89. $root = Navigation::loadPageTree(1, true, false);
  90. $this->assertCount(4, $root->children);
  91. $this->assertEquals('Top level one - std live', $root->children[0]['name']);
  92. $this->assertEquals('Top level two - std live', $root->children[1]['name']);
  93. $this->assertEquals('Top level three - inactive', $root->children[2]['name']);
  94. $this->assertEquals('Top level four - unpublished', $root->children[3]['name']);
  95. }
  96.  
  97. /**
  98.   * Front-end loading should not bring in inactive pages
  99.   */
  100. public function testLoadPageTree__FrontEnd()
  101. {
  102. $root = Navigation::loadPageTree(1, false, false);
  103. $this->assertCount(2, $root->children);
  104. $this->assertEquals('Top level one - std live', $root->children[0]['name']);
  105. $this->assertEquals('Top level two - std live', $root->children[1]['name']);
  106. }
  107.  
  108.  
  109. public function dataCustomBreadcrumb()
  110. {
  111. return array(
  112. array(array('one'), '<a href="SITE/">Home</a> &raquo; <span>one</span>'),
  113. array(array('aaa' => 'one'), '<a href="SITE/">Home</a> &raquo; <span>one</span>'),
  114.  
  115. array(array('one', 'two'), '<a href="SITE/">Home</a> &raquo; <a href="">one</a> &raquo; <span>two</span>'),
  116. array(array('aaa' => 'one', 'two'), '<a href="SITE/">Home</a> &raquo; <a href="aaa">one</a> &raquo; <span>two</span>'),
  117. array(array('one', 'aaa' => 'two'), '<a href="SITE/">Home</a> &raquo; <a href="">one</a> &raquo; <span>two</span>'),
  118. array(array('aaa' => 'one', 'bbb' => 'two'), '<a href="SITE/">Home</a> &raquo; <a href="aaa">one</a> &raquo; <span>two</span>'),
  119.  
  120. array(array('aaa' => 'o&e', 'two'), '<a href="SITE/">Home</a> &raquo; <a href="aaa">o&amp;e</a> &raquo; <span>two</span>'),
  121. array(array('aaa' => 'o&e', 't&o'), '<a href="SITE/">Home</a> &raquo; <a href="aaa">o&amp;e</a> &raquo; <span>t&amp;o</span>'),
  122. array(array('aaa' => 'one', 't&o'), '<a href="SITE/">Home</a> &raquo; <a href="aaa">one</a> &raquo; <span>t&amp;o</span>'),
  123.  
  124. array(array('a&a' => 'one', 'two'), '<a href="SITE/">Home</a> &raquo; <a href="a&amp;a">one</a> &raquo; <span>two</span>'),
  125. );
  126. }
  127.  
  128.  
  129. /**
  130.   * @dataProvider dataCustomBreadcrumb
  131.   **/
  132. public function testCustomBreadcrumb($crumbs, $expected)
  133. {
  134. $actual = Navigation::customBreadcrumb($crumbs);
  135. $this->assertEquals($expected, $actual);
  136. }
  137.  
  138. }
  139.