f7a6de896e801d7da2de728e395f0352491f0345
[yaffs-website] / web / modules / contrib / diff / tests / src / Unit / VisualDiffThemeNegotiatorTest.php
1 <?php
2
3 namespace Drupal\Tests\diff\Unit;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Config\ImmutableConfig;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\diff\VisualDiffThemeNegotiator;
9 use Drupal\Tests\UnitTestCase;
10
11 /**
12  * Tests theme negotiator.
13  *
14  * @coversDefaultClass \Drupal\diff\VisualDiffThemeNegotiator
15  * @group diff
16  */
17 class VisualDiffThemeNegotiatorTest extends UnitTestCase {
18
19   /**
20    * The config factory.
21    *
22    * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
23    */
24   protected $configFactory;
25
26   /**
27    * The class under test.
28    *
29    * @var \Drupal\diff\VisualDiffThemeNegotiator
30    */
31   protected $themeNegotiator;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38
39     $this->configFactory = $this->prophesize(ConfigFactoryInterface::class);
40     $this->themeNegotiator = new VisualDiffThemeNegotiator($this->configFactory->reveal());
41   }
42
43   /**
44    * @covers ::determineActiveTheme
45    */
46   public function testDetermineActiveTheme() {
47     $config = $this->prophesize(ImmutableConfig::class);
48     $config->get('default')->willReturn('the_default_theme');
49     $this->configFactory->get('system.theme')->willReturn($config->reveal());
50
51     $route_match = $this->prophesize(RouteMatchInterface::class);
52     $result = $this->themeNegotiator->determineActiveTheme($route_match->reveal());
53     $this->assertSame('the_default_theme', $result);
54   }
55
56   /**
57    * Tests if the theme negotiator applies under correct conditions.
58    *
59    * @param string $filter_parameter
60    *   The filter parameter.
61    * @param string $route_name
62    *   The route name.
63    * @param string $config_value
64    *   The configuration value of the element taken from the form values.
65    * @param bool $expected
66    *   The expected result.
67    *
68    * @covers ::applies
69    * @covers ::isDiffRoute
70    *
71    * @dataProvider providerTestApplies
72    */
73   public function testApplies($filter_parameter, $route_name, $config_value, $expected) {
74     $route_match = $this->prophesize(RouteMatchInterface::class);
75     $route_match->getParameter('filter')->willReturn($filter_parameter);
76
77     if ($route_name) {
78       $route_match->getRouteName()->willReturn($route_name);
79     }
80     else {
81       $route_match->getRouteName()->shouldNotBeCalled();
82     }
83
84     if ($config_value) {
85       $diff_config = $this->prophesize(ImmutableConfig::class);
86       $diff_config->get('general_settings.visual_inline_theme')->willReturn($config_value);
87       $this->configFactory->get('diff.settings')->willReturn($diff_config->reveal());
88     }
89     else {
90       $this->configFactory->get('diff.settings')->shouldNotBeCalled();
91     }
92
93     $this->assertSame($expected, $this->themeNegotiator->applies($route_match->reveal()));
94   }
95
96   /**
97    * Provides test data to ::testApplies().
98    */
99   public function providerTestApplies() {
100     $data = [];
101     $data[] = [
102       'unexpected_filter_parameter',
103       NULL,
104       NULL,
105       FALSE,
106     ];
107     $data[] = [
108       'visual_inline',
109       'unexpected_route_name',
110       NULL,
111       FALSE,
112     ];
113     $data[] = [
114       'visual_inline',
115       'diff.revisions_diff',
116       'unexpected_config_value',
117       FALSE,
118     ];
119     $data[] = [
120       'visual_inline',
121       'diff.revisions_diff',
122       'default',
123       TRUE,
124     ];
125     $data[] = [
126       'visual_inline',
127       'entity.foo.revisions_diff',
128       'default',
129       TRUE,
130     ];
131     return $data;
132   }
133
134 }