SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/adminPermsTest.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.  
  14. use Sprout\Helpers\AdminPerms;
  15. use Sprout\Helpers\Constants;
  16. use Sprout\Helpers\Pdb;
  17.  
  18.  
  19. class adminPermsTest extends PHPUnit_Framework_TestCase
  20. {
  21.  
  22. /**
  23.   * Create a page structure in memory (SQLite)
  24.   * [1] [6] [9]
  25.   * [2] [3] [4] [5] [7] [8] [10] [11]
  26.   */
  27. protected function setUp()
  28. {
  29. $pages = [
  30. ['id' => 1, 'parent_id' => 0, 'admin_perm_type' => Constants::PERM_INHERIT], // top-level, all
  31. ['id' => 2, 'parent_id' => 1, 'admin_perm_type' => Constants::PERM_INHERIT], // inherit, all
  32. ['id' => 3, 'parent_id' => 1, 'admin_perm_type' => Constants::PERM_SPECIFIC], // no perms
  33. ['id' => 4, 'parent_id' => 1, 'admin_perm_type' => Constants::PERM_SPECIFIC], // group 1
  34. ['id' => 5, 'parent_id' => 1, 'admin_perm_type' => Constants::PERM_SPECIFIC], // group 2
  35.  
  36. ['id' => 6, 'parent_id' => 0, 'admin_perm_type' => Constants::PERM_SPECIFIC], // top-level, none
  37. ['id' => 7, 'parent_id' => 6, 'admin_perm_type' => Constants::PERM_INHERIT], // inherit, none
  38. ['id' => 8, 'parent_id' => 6, 'admin_perm_type' => Constants::PERM_SPECIFIC], // specific, group 1
  39.  
  40. ['id' => 9, 'parent_id' => 0, 'admin_perm_type' => Constants::PERM_SPECIFIC], // top-level, group 1
  41. ['id' => 10,'parent_id' => 9, 'admin_perm_type' => Constants::PERM_INHERIT], // inherit, group 1
  42. ['id' => 11,'parent_id' => 9, 'admin_perm_type' => Constants::PERM_SPECIFIC], // specific, group 2
  43. ];
  44. $cats = [
  45. ['id' => 1, 'name' => 'Group one'],
  46. ['id' => 2, 'name' => 'Group two'],
  47. ];
  48. $perms = [
  49. ['item_id' => 4, 'category_id' => 1],
  50. ['item_id' => 5, 'category_id' => 2],
  51. ['item_id' => 8, 'category_id' => 1],
  52. ['item_id' => 9, 'category_id' => 1],
  53. ['item_id' => 11,'category_id' => 2],
  54. ];
  55.  
  56. $sqlite = new PDO('sqlite::memory:');
  57. Pdb::setOverrideConnection($sqlite);
  58.  
  59. Pdb::query('CREATE TABLE ~pages (id INT, parent_id INT, admin_perm_type INT)', [], 'null');
  60. foreach ($pages as $row) {
  61. Pdb::insert('pages', $row);
  62. }
  63.  
  64. Pdb::query('CREATE TABLE ~operators_cat_list (id INT, name VARCHAR(100))', [], 'null');
  65. foreach ($cats as $row) {
  66. Pdb::insert('operators_cat_list', $row);
  67. }
  68.  
  69. Pdb::query('CREATE TABLE ~page_admin_permissions (item_id INT, category_id INT)', [], 'null');
  70. foreach ($perms as $row) {
  71. Pdb::insert('page_admin_permissions', $row);
  72. }
  73. }
  74.  
  75. protected function tearDown()
  76. {
  77. Pdb::clearOverrideConnection();
  78. }
  79.  
  80.  
  81. public function dataGetAccessableGroups()
  82. {
  83. return [
  84. [0, [1, 2]],
  85. [1, [1, 2]],
  86. [2, [1, 2]],
  87. [3, []],
  88. [4, [1]],
  89. [5, [2]],
  90. [6, []],
  91. [7, []],
  92. [8, [1]],
  93. [9, [1]],
  94. [10,[1]],
  95. [11,[2]],
  96. [99999, false],
  97. ];
  98. }
  99.  
  100.  
  101. /**
  102.   * @dataProvider dataGetAccessableGroups
  103.   */
  104. public function testGetAccessableGroups($page_id, $expected)
  105. {
  106. $this->assertEquals($expected, AdminPerms::getAccessableGroups('pages', $page_id));
  107. }
  108.  
  109. }
  110.