Pull merge.
[yaffs-website] / web / core / modules / aggregator / tests / src / Unit / Plugin / AggregatorPluginSettingsBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Unit\Plugin;
4
5 use Drupal\aggregator\Form\SettingsForm;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Messenger\MessengerInterface;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * Tests settings configuration of individual aggregator plugins.
12  *
13  * @group aggregator
14  */
15 class AggregatorPluginSettingsBaseTest extends UnitTestCase {
16
17   /**
18    * The aggregator settings form object under test.
19    *
20    * @var \Drupal\aggregator\Form\SettingsForm
21    */
22   protected $settingsForm;
23
24   /**
25    * The stubbed config factory object.
26    *
27    * @var \PHPUnit_Framework_MockObject_MockBuilder
28    */
29   protected $configFactory;
30
31   /**
32    * The stubbed aggregator plugin managers array.
33    *
34    * @var array
35    */
36   protected $managers;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     $this->configFactory = $this->getConfigFactoryStub(
43       [
44         'aggregator.settings' => [
45           'processors' => ['aggregator_test'],
46         ],
47         'aggregator_test.settings' => [],
48       ]
49     );
50     foreach (['fetcher', 'parser', 'processor'] as $type) {
51       $this->managers[$type] = $this->getMockBuilder('Drupal\aggregator\Plugin\AggregatorPluginManager')
52         ->disableOriginalConstructor()
53         ->getMock();
54       $this->managers[$type]->expects($this->once())
55         ->method('getDefinitions')
56         ->will($this->returnValue(['aggregator_test' => ['title' => '', 'description' => '']]));
57     }
58
59     /** @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockBuilder $messenger */
60     $messenger = $this->createMock(MessengerInterface::class);
61     $messenger->expects($this->any())->method('addMessage');
62
63     $this->settingsForm = new SettingsForm(
64       $this->configFactory,
65       $this->managers['fetcher'],
66       $this->managers['parser'],
67       $this->managers['processor'],
68       $this->getStringTranslationStub()
69     );
70     $this->settingsForm->setMessenger($messenger);
71   }
72
73   /**
74    * Test for AggregatorPluginSettingsBase.
75    *
76    * Ensure that the settings form calls build, validate and submit methods on
77    * plugins that extend AggregatorPluginSettingsBase.
78    */
79   public function testSettingsForm() {
80     // Emulate a form state of a submitted form.
81     $form_state = (new FormState())->setValues([
82       'dummy_length' => '',
83       'aggregator_allowed_html_tags' => '',
84     ]);
85
86     $test_processor = $this->getMock(
87       'Drupal\aggregator_test\Plugin\aggregator\processor\TestProcessor',
88       ['buildConfigurationForm', 'validateConfigurationForm', 'submitConfigurationForm'],
89       [[], 'aggregator_test', ['description' => ''], $this->configFactory]
90     );
91     $test_processor->expects($this->at(0))
92       ->method('buildConfigurationForm')
93       ->with($this->anything(), $form_state)
94       ->will($this->returnArgument(0));
95     $test_processor->expects($this->at(1))
96       ->method('validateConfigurationForm')
97       ->with($this->anything(), $form_state);
98     $test_processor->expects($this->at(2))
99       ->method('submitConfigurationForm')
100       ->with($this->anything(), $form_state);
101
102     $this->managers['processor']->expects($this->once())
103       ->method('createInstance')
104       ->with($this->equalTo('aggregator_test'))
105       ->will($this->returnValue($test_processor));
106
107     $form = $this->settingsForm->buildForm([], $form_state);
108     $this->settingsForm->validateForm($form, $form_state);
109     $this->settingsForm->submitForm($form, $form_state);
110   }
111
112 }