CookieconsentPluginTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Grav\Plugin;
  3. require_once __DIR__ . '/../../cookieconsent.php';
  4. use Codeception\Util\Stub;
  5. use Grav\Common\Assets;
  6. use Grav\Common\Config\Config;
  7. use Grav\Common\Grav;
  8. use Grav\Common\Twig\Twig;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. /**
  11. * Class CookieconsentPluginTest
  12. */
  13. class CookieconsentPluginTest extends \Codeception\Test\Unit
  14. {
  15. /**
  16. * @var Grav
  17. */
  18. protected $grav;
  19. /**
  20. * @var Config
  21. */
  22. protected $config;
  23. /**
  24. * @var EventDispatcherInterface
  25. */
  26. protected $eventDispatcher;
  27. /**
  28. * @var Twig
  29. */
  30. protected $twig;
  31. /**
  32. * @var \Twig\Environment
  33. */
  34. protected $twigEngine;
  35. /**
  36. * @var Assets
  37. */
  38. protected $assets;
  39. protected function _before()
  40. {
  41. $this->grav = Stub::make(Grav::class);
  42. $this->config = Stub::make(Config::class);
  43. $this->eventDispatcher = Stub::makeEmpty(EventDispatcherInterface::class);
  44. $this->twigEngine = Stub::makeEmpty(\Twig\Environment::class);
  45. $this->twig = Stub::make(Twig::class);
  46. $this->assets = Stub::make(Assets::class);
  47. }
  48. public function testGetSubscribedEvents()
  49. {
  50. $expectedResult = [
  51. 'onPluginsInitialized' => ['onPluginsInitialized', 0]
  52. ];
  53. $actualResult = CookieconsentPlugin::getSubscribedEvents();
  54. $this->assertEquals($expectedResult, $actualResult);
  55. }
  56. public function testOnPluginsInitialized()
  57. {
  58. $this->grav['events'] = $this->eventDispatcher;
  59. $plugin = new CookieconsentPlugin('Cookie Consent', $this->grav, $this->config);
  60. $plugin->onPluginsInitialized();
  61. }
  62. public function testOnTwigTemplatePaths()
  63. {
  64. $this->grav['twig'] = $this->twig;
  65. $plugin = new CookieconsentPlugin('Cookie Consent', $this->grav, $this->config);
  66. $plugin->onTwigTemplatePaths();
  67. $this->assertCount(1, $this->twig->twig_paths);
  68. $this->assertContains('/templates', $this->twig->twig_paths[0]);
  69. }
  70. public function testOnTwigSiteVariables()
  71. {
  72. $this->grav['twig'] = $this->twig;
  73. $this->twig->twig = $this->twigEngine;
  74. $this->config = Stub::make(Config::class,
  75. [
  76. 'get' => function($key) {
  77. if ($key == 'plugins.cookieconsent.cdn') {
  78. return true;
  79. }
  80. return null;
  81. }
  82. ]
  83. );
  84. $this->assets = Stub::make(Assets::class,
  85. [
  86. 'addCss' => function($asset) {
  87. $this->assertEquals('//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.6/cookieconsent.min.css', $asset);
  88. },
  89. 'addJs' => function($asset) {
  90. $this->assertEquals('//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.6/cookieconsent.min.js', $asset);
  91. }
  92. ]
  93. );
  94. $this->grav['assets'] = $this->assets;
  95. $plugin = new CookieconsentPlugin('Cookie Consent', $this->grav, $this->config);
  96. $plugin->onTwigSiteVariables();
  97. }
  98. public function testOnTwigSiteVariablesWithDisabledCdn()
  99. {
  100. $this->grav['twig'] = $this->twig;
  101. $this->twig->twig = $this->twigEngine;
  102. $this->config = Stub::make(Config::class,
  103. [
  104. 'get' => function($key) {
  105. if ($key == 'plugins.cookieconsent.cdn') {
  106. return false;
  107. }
  108. return null;
  109. }
  110. ]
  111. );
  112. $this->assets = Stub::make(Assets::class,
  113. [
  114. 'addCss' => function($asset) {
  115. $this->assertEquals('plugin://cookieconsent/assets/cookieconsent.min.css', $asset);
  116. },
  117. 'addJs' => function($asset) {
  118. $this->assertEquals('plugin://cookieconsent/assets/cookieconsent.min.js', $asset);
  119. }
  120. ]
  121. );
  122. $this->grav['assets'] = $this->assets;
  123. $plugin = new CookieconsentPlugin('Cookie Consent', $this->grav, $this->config);
  124. $plugin->onTwigSiteVariables();
  125. }
  126. }