<commit_before>describe('module: Permission', function () {
  'use strict';

  var $rootScope, $state, $stateProvider, $q, PermissionProvider;

  beforeEach(function () {
    module('ui.router', function ($injector) {
      $stateProvider = $injector.get('$stateProvider');
    });

    module('permission', function ($injector) {
      PermissionProvider = $injector.get('PermissionProvider');
    });

    inject(function ($injector) {
      $state = $injector.get('$state');
      $q = $injector.get('$q');
      $rootScope = $injector.get('$rootScope');
    });
  });

  // Initialize permissions
  beforeEach(function () {
    PermissionProvider.setPermission('accepted', function () {
      return true;
    });

    PermissionProvider.setPermission('denied', function () {
      return false;
    });

    PermissionProvider.setPermission('withParams', function (params) {
      if (params.isSet && angular.isString(params.isSet)) {
        return params.isSet === 'true';
      }
      else {
        return params.isSet === true;
      }
    });
  });

  // Set default states and go home
  beforeEach(function () {
    $stateProvider
      .state('home', {})
      .state('accepted', {
        data: {
          permissions: {only: ['accepted']}
        }
      })
      .state('denied', {
        data: {
          permissions: {
            only: ['denied']
          }
        }
      });

    $state.go('home');
    $rootScope.$apply();
  });

  var changePermissionAcceptedHasBeenCalled, changePermissionDeniedHasBeenCalled;

  // Bind event listeners
  beforeEach(function () {
    changePermissionAcceptedHasBeenCalled = false;
    changePermissionDeniedHasBeenCalled = false;

    $rootScope.$on('$stateChangePermissionAccepted', function () {
      changePermissionAcceptedHasBeenCalled = true;
    });

    $rootScope.$on('$stateChangePermissionDenied', function () {
      changePermissionDeniedHasBeenCalled = true;
    });
  });

  describe('event: $stateChangeStart', function () {
    it('should go to an accepted state', function () {
      // GIVEN
      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      // THEN
      expect($state.current.name).toBe('accepted');
      expect(changePermissionAcceptedHasBeenCalled).toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
    });

    it('should broadcast a $stateChangeStart with correct parameters (accepted state)', function () {
      // GIVEN
      var called = false;
      var toState = null;
      var fromState = null;

      $rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState) {
        called = true;
        toState = _toState;
        fromState = _fromState;
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      expect(called).toBeTruthy();
      expect(toState.name).toBe('accepted');
      expect(fromState.name).toBe('home');
    });

    it('should broadcast a $stateChangePermissionStart', inject(function ($rootScope) {
      // GIVEN
      var called = false;
      var toState = null;

      $rootScope.$on('$stateChangePermissionStart', function (event, _toState) {
        called = true;
        toState = _toState;
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      //THEN
      expect(called).toBeTruthy();
      expect(toState.name).toBe('accepted');
    }));

    it('should not go to a state when $stateChangePermissionStart has been cancelled', function () {
      // GIVEN
      $rootScope.$on('$stateChangePermissionStart', function (event) {
        //noinspection JSUnresolvedFunction
        event.preventDefault();
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      // THEN
      expect($state.current.name).toBe('home');
      expect(changePermissionAcceptedHasBeenCalled).not.toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
    });

    it('should not go to the denied state', function () {
      // GIVEN
      // WHEN
      $state.go('denied');
      $rootScope.$digest();

      // THEN
      expect($state.current.name).toBe('home');
      expect(changePermissionAcceptedHasBeenCalled).not.toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).toBeTruthy();
    });

    it('should broadcast a $stateChangeStart with correct parameters (denied state)', inject(function ($rootScope) {
      // GIVEN
      var called = false;
      var toState = null;
      var fromState = null;

      $rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState) {
        called = true;
        toState = _toState;
        fromState = _fromState;
      });

      // WHEN
      $state.go('denied');
      $rootScope.$apply();

      // THEN<commit_msg>Extended test to replicate the #114 case.<commit_after>describe('module: Permission', function () {
  'use strict';

  var $rootScope, $state, $stateProvider, $q, PermissionProvider;

  beforeEach(function () {
    module('ui.router', function ($injector) {
      $stateProvider = $injector.get('$stateProvider');
    });

    module('permission', function ($injector) {
      PermissionProvider = $injector.get('PermissionProvider');
    });

    inject(function ($injector) {
      $state = $injector.get('$state');
      $q = $injector.get('$q');
      $rootScope = $injector.get('$rootScope');
    });
  });

  // Initialize permissions
  beforeEach(function () {
    PermissionProvider.setPermission('accepted', function () {
      return true;
    });

    PermissionProvider.setPermission('denied', function () {
      return false;
    });

    PermissionProvider.setPermission('withParams', function (params) {
      if (params.isSet && angular.isString(params.isSet)) {
        return params.isSet === 'true';
      }
      else {
        return params.isSet === true;
      }
    });
  });

  // Set default states and go home
  beforeEach(function () {
    $stateProvider
      .state('home', {})
      .state('accepted', {
        data: {
          permissions: {only: ['accepted']}
        }
      })
      .state('denied', {
        data: {
          permissions: {
            only: ['denied']
          }
        }
      });

    $state.go('home');
    $rootScope.$apply();
  });

  var changePermissionAcceptedHasBeenCalled, changePermissionDeniedHasBeenCalled;

  // Bind event listeners
  beforeEach(function () {
    changePermissionAcceptedHasBeenCalled = false;
    changePermissionDeniedHasBeenCalled = false;

    $rootScope.$on('$stateChangePermissionAccepted', function () {
      changePermissionAcceptedHasBeenCalled = true;
    });

    $rootScope.$on('$stateChangePermissionDenied', function () {
      changePermissionDeniedHasBeenCalled = true;
    });
  });

  describe('event: $stateChangeStart', function () {
    it('should go to an accepted state', function () {
      // GIVEN
      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      // THEN
      expect($state.current.name).toBe('accepted');
      expect(changePermissionAcceptedHasBeenCalled).toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
    });

    it('should broadcast a $stateChangeStart with correct parameters (accepted state)', function () {
      // GIVEN
      var called = false;
      var toState = null;
      var fromState = null;

      $rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState) {
        called = true;
        toState = _toState;
        fromState = _fromState;
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      expect(called).toBeTruthy();
      expect(toState.name).toBe('accepted');
      expect(fromState.name).toBe('home');
    });

    it('should broadcast a $stateChangePermissionStart', inject(function ($rootScope) {
      // GIVEN
      var called = false;
      var toState = null;

      $rootScope.$on('$stateChangePermissionStart', function (event, _toState) {
        called = true;
        toState = _toState;
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      //THEN
      expect(called).toBeTruthy();
      expect(toState.name).toBe('accepted');
    }));

    it('should not go to a state when $stateChangePermissionStart has been cancelled', function () {
      // GIVEN
      $rootScope.$on('$stateChangePermissionStart', function (event) {
        //noinspection JSUnresolvedFunction
        event.preventDefault();
      });

      // WHEN
      $state.go('accepted');
      $rootScope.$apply();

      // THEN
      expect($state.current.name).toBe('home');
      expect(changePermissionAcceptedHasBeenCalled).not.toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
    });

    it('should not go to the denied state', function () {
      // GIVEN
      // WHEN
      $state.go('denied');
      $rootScope.$digest();

      // THEN
      expect($state.current.name).toBe('home');
      expect(changePermissionAcceptedHasBeenCalled).not.toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).toBeTruthy();
    });

    it('should go to child state if parent permissions are resolved', function () {
      // GIVEN
      $stateProvider.state('accepted.acceptedChild', {});

      // WHEN
      $state.go('accepted.acceptedChild');
      $rootScope.$digest();

      // THEN
      expect($state.current.name).toBe('accepted.acceptedChild');
      expect(changePermissionAcceptedHasBeenCalled).toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
    });

    it('should not go to child state if parent permissions are rejected', function () {
      // GIVEN
      $stateProvider.state('denied.deniedChild', {});

      // WHEN
      $state.go('denied.deniedChild');
      $rootScope.$digest();

      // THEN
      expect($state.current.name).toBe('home');
      expect(changePermissionAcceptedHasBeenCalled).not.toBeTruthy();
      expect(changePermissionDeniedHasBeenCalled).toBeTruthy();
    });

    it('should broadcast a $stateChangeStart with correct parameters (denied state)', inject(function ($rootScope) {
      // GIVEN
      var called = false;
      var toState = null;
      var fromState = null;

      $rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState) {
        called = true;
        toState = _toState;
        fromState = _fromState;
      });

      // WHEN
      $state.go('denied');
      $rootScope.$apply();

      // THEN
{'fraction_non_alphanumeric': 0.14489873178118493, 'fraction_numerical': 0.00028392958546280523, 'mean_word_length': 2.723396758280479, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4910117', 'n_tokens_mistral': 3090, 'n_tokens_neox': 3011, 'n_words': 558}