<?php

require_once __DIR__ . '/../../../autoload/Domain/Users/PermissionRepository.php';

use Domain\Users\PermissionRepository;

function assert_perm( $expected, $actual, $message )
{
  if ( $expected !== $actual )
    throw new Exception( $message . " (expected " . var_export( $expected, true ) . ", got " . var_export( $actual, true ) . ")" );
}

function run_permission_repository_tests()
{
  // Test MODULES constant
  $modules = PermissionRepository::MODULES;
  assert_perm( true, in_array( 'tasks', $modules ), 'MODULES should contain tasks' );
  assert_perm( true, in_array( 'projects_view', $modules ), 'MODULES should contain projects_view' );
  assert_perm( true, in_array( 'projects_add', $modules ), 'MODULES should contain projects_add' );
  assert_perm( true, in_array( 'projects_edit', $modules ), 'MODULES should contain projects_edit' );
  assert_perm( true, in_array( 'projects_delete', $modules ), 'MODULES should contain projects_delete' );
  assert_perm( true, in_array( 'finances', $modules ), 'MODULES should contain finances' );
  assert_perm( 9, count( $modules ), 'MODULES should have 9 entries' );
  assert_perm( false, in_array( 'zaplecze', $modules ), 'MODULES should not contain zaplecze' );

  // Test DEFAULTS constant
  $defaults = PermissionRepository::DEFAULTS;
  assert_perm( 1, $defaults['tasks'], 'tasks should default to 1' );
  assert_perm( 1, $defaults['projects_view'], 'projects_view should default to 1' );
  assert_perm( 1, $defaults['projects_add'], 'projects_add should default to 1' );
  assert_perm( 1, $defaults['projects_edit'], 'projects_edit should default to 1' );
  assert_perm( 1, $defaults['projects_delete'], 'projects_delete should default to 1' );
  assert_perm( 0, $defaults['finances'], 'finances should default to 0' );
  assert_perm( 0, $defaults['crm'], 'crm should default to 0' );

  // Test defaults() returns full module array
  $result = PermissionRepository::defaults();
  assert_perm( 9, count( $result ), 'defaults() should return 9 modules' );
  assert_perm( 1, $result['tasks'], 'defaults() tasks should be 1' );
}
