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