summaryrefslogtreecommitdiffstats
path: root/core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
blob: 56c0339bd67327d24670ed01596dafd1d082380f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace Drupal\Tests\aggregator\Unit\Plugin;

use Drupal\aggregator\Form\SettingsForm;
use Drupal\Core\Form\FormState;
use Drupal\Tests\UnitTestCase;

/**
 * Tests settings configuration of individual aggregator plugins.
 *
 * @group aggregator
 */
class AggregatorPluginSettingsBaseTest extends UnitTestCase {

  /**
   * The aggregator settings form object under test.
   *
   * @var \Drupal\aggregator\Form\SettingsForm
   */
  protected $settingsForm;

  /**
   * The stubbed config factory object.
   *
   * @var \PHPUnit_Framework_MockObject_MockBuilder
   */
  protected $configFactory;

  /**
   * The stubbed aggregator plugin managers array.
   *
   * @var array
   */
  protected $managers;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->configFactory = $this->getConfigFactoryStub(
      [
        'aggregator.settings' => [
          'processors' => ['aggregator_test'],
        ],
        'aggregator_test.settings' => [],
      ]
    );
    foreach (['fetcher', 'parser', 'processor'] as $type) {
      $this->managers[$type] = $this->getMockBuilder('Drupal\aggregator\Plugin\AggregatorPluginManager')
        ->disableOriginalConstructor()
        ->getMock();
      $this->managers[$type]->expects($this->once())
        ->method('getDefinitions')
        ->will($this->returnValue(['aggregator_test' => ['title' => '', 'description' => '']]));
    }

    $this->settingsForm = new SettingsForm(
      $this->configFactory,
      $this->managers['fetcher'],
      $this->managers['parser'],
      $this->managers['processor'],
      $this->getStringTranslationStub()
    );
  }

  /**
   * Test for AggregatorPluginSettingsBase.
   *
   * Ensure that the settings form calls build, validate and submit methods on
   * plugins that extend AggregatorPluginSettingsBase.
   */
  public function testSettingsForm() {
    // Emulate a form state of a submitted form.
    $form_state = (new FormState())->setValues([
      'dummy_length' => '',
      'aggregator_allowed_html_tags' => '',
    ]);

    $test_processor = $this->getMock(
      'Drupal\aggregator_test\Plugin\aggregator\processor\TestProcessor',
      ['buildConfigurationForm', 'validateConfigurationForm', 'submitConfigurationForm'],
      [[], 'aggregator_test', ['description' => ''], $this->configFactory]
    );
    $test_processor->expects($this->at(0))
      ->method('buildConfigurationForm')
      ->with($this->anything(), $form_state)
      ->will($this->returnArgument(0));
    $test_processor->expects($this->at(1))
      ->method('validateConfigurationForm')
      ->with($this->anything(), $form_state);
    $test_processor->expects($this->at(2))
      ->method('submitConfigurationForm')
      ->with($this->anything(), $form_state);

    $this->managers['processor']->expects($this->once())
      ->method('createInstance')
      ->with($this->equalTo('aggregator_test'))
      ->will($this->returnValue($test_processor));

    $form = $this->settingsForm->buildForm([], $form_state);
    $this->settingsForm->validateForm($form, $form_state);
    $this->settingsForm->submitForm($form, $form_state);
  }

}

// @todo Delete after https://www.drupal.org/node/2278383 is in.
namespace Drupal\Core\Form;

if (!function_exists('drupal_set_message')) {
  function drupal_set_message() {
  }
}