From 8a8a3c7779a03ac83dcbc0b015e53062cbf0aef4 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Tue, 3 Oct 2023 16:24:28 -0700 Subject: [PATCH] more test coverage --- src/EventSubscriber/EventSubscriber.php | 2 +- .../EventSubscriber/EventSubscriberTest.php | 47 +++++++++++++++++++ .../test_stanford_profile.info.yml | 6 +++ .../test_stanford_profile.routing.yml | 4 ++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/test_stanford_profile/test_stanford_profile.info.yml create mode 100644 tests/test_stanford_profile/test_stanford_profile.routing.yml diff --git a/src/EventSubscriber/EventSubscriber.php b/src/EventSubscriber/EventSubscriber.php index acaff2cf3..06b9365a1 100644 --- a/src/EventSubscriber/EventSubscriber.php +++ b/src/EventSubscriber/EventSubscriber.php @@ -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; diff --git a/tests/src/Kernel/EventSubscriber/EventSubscriberTest.php b/tests/src/Kernel/EventSubscriber/EventSubscriberTest.php index 8e47f4a1d..c9e3cf5ba 100644 --- a/tests/src/Kernel/EventSubscriber/EventSubscriberTest.php +++ b/tests/src/Kernel/EventSubscriber/EventSubscriberTest.php @@ -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. @@ -33,6 +41,7 @@ class EventSubscriberTest extends KernelTestBase { 'simple_oauth', 'serialization', 'media', + 'test_stanford_profile', ]; /** @@ -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"); + } + } + } /** diff --git a/tests/test_stanford_profile/test_stanford_profile.info.yml b/tests/test_stanford_profile/test_stanford_profile.info.yml new file mode 100644 index 000000000..90c2ffcd6 --- /dev/null +++ b/tests/test_stanford_profile/test_stanford_profile.info.yml @@ -0,0 +1,6 @@ +name: 'Test Stanford Profile' +type: module +description: 'Stanford Profile Module.' +core_version_requirement: ^9 || ^10 +package: Testing +hidden: true diff --git a/tests/test_stanford_profile/test_stanford_profile.routing.yml b/tests/test_stanford_profile/test_stanford_profile.routing.yml new file mode 100644 index 000000000..ccd0fe4b0 --- /dev/null +++ b/tests/test_stanford_profile/test_stanford_profile.routing.yml @@ -0,0 +1,4 @@ +config_pages.stanford_basic_site_settings: + path: '/' + requirements: + _access: true