Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Oct 9, 2023
1 parent 37901b8 commit 8a8a3c7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/EventSubscriber/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected static function redirectUser() {
$site_manager = $current_user->hasPermission('edit stanford_basic_site_settings config page entity') && !in_array('administrator', $current_user->getRoles());

// If the renewal date has passed, they should be redirected.
$needs_renewal = $site_manager && strtotime($renewal_date) - time();
$needs_renewal = $site_manager && (strtotime($renewal_date) - time() < 60 * 60 * 24);
$cache->set('su_renew_site', $needs_renewal, time() + 60 * 60 * 24);

return $needs_renewal;
Expand Down
47 changes: 47 additions & 0 deletions tests/src/Kernel/EventSubscriber/EventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Drupal\Tests\stanford_profile\Kernel\EventSubscriber;

use Drupal\config_pages\ConfigPagesLoaderServiceInterface;
use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\core_event_dispatcher\Event\Entity\EntityInsertEvent;
use Drupal\default_content\Event\ImportEvent;
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\media\Entity\MediaType;
use Drupal\stanford_profile\EventSubscriber\EventSubscriber as StanfordEventSubscriber;
use Drupal\user\Entity\Role;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Class EventSubscriberTest.
Expand All @@ -33,6 +41,7 @@ class EventSubscriberTest extends KernelTestBase {
'simple_oauth',
'serialization',
'media',
'test_stanford_profile',
];

/**
Expand Down Expand Up @@ -113,6 +122,44 @@ public function testContentImportEntity() {
$this->assertFileExists('public://foobar.jpg');
}

public function testUserInsert() {
\Drupal::service('module_installer')->install(['samlauth']);
$role = Role::create(['id' => 'test_role1', 'label' => 'Test role 1']);
$role->save();

$event = new EntityInsertEvent($role);
$this->eventSubscriber->onEntityInsert($event);
$saml_setting = \Drupal::config('samlauth.authentication')
->get('map_users_roles');

$this->assertContains('test_role1', $saml_setting);
}

public function testKernelRequest() {
$config_page_loader = $this->createMock(ConfigPagesLoaderServiceInterface::class);

\Drupal::getContainer()->set('config_pages.loader', $config_page_loader);

$account = $this->createMock(AccountProxyInterface::class);
$account->method('hasPermission')->willReturn(TRUE);
$account->method('getRoles')->willReturn([]);

\Drupal::currentUser()->setAccount($account);

$ci = getenv('CI');
$request = Request::create('/foo/bar', 'GET', [], [], [], ['SCRIPT_NAME' => 'index.php']);

$http_kernel = $this->createMock(HttpKernelInterface::class);
$event = new RequestEvent($http_kernel, $request, HttpKernelInterface::MAIN_REQUEST);

$this->eventSubscriber->onKernelRequest($event);
$this->assertInstanceOf(RedirectResponse::class, $event->getResponse());

if ($ci) {
putenv("CI=$ci");
}
}

}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/test_stanford_profile/test_stanford_profile.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'Test Stanford Profile'
type: module
description: 'Stanford Profile Module.'
core_version_requirement: ^9 || ^10
package: Testing
hidden: true
4 changes: 4 additions & 0 deletions tests/test_stanford_profile/test_stanford_profile.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config_pages.stanford_basic_site_settings:
path: '/'
requirements:
_access: true

0 comments on commit 8a8a3c7

Please sign in to comment.