AggregatorPluginSettingsBaseTest.php 3.39 KB
Newer Older
1 2
<?php

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 9 10
use Drupal\Tests\UnitTestCase;

/**
11
 * Tests settings configuration of individual aggregator plugins.
12
 *
13
 * @group aggregator
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */
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;

38 39 40
  /**
   * {@inheritdoc}
   */
41
  protected function setUp() {
42
    $this->configFactory = $this->getConfigFactoryStub(
43 44 45 46 47 48
      [
        'aggregator.settings' => [
          'processors' => ['aggregator_test'],
        ],
        'aggregator_test.settings' => [],
      ]
49
    );
50
    foreach (['fetcher', 'parser', 'processor'] as $type) {
51 52 53 54 55
      $this->managers[$type] = $this->getMockBuilder('Drupal\aggregator\Plugin\AggregatorPluginManager')
        ->disableOriginalConstructor()
        ->getMock();
      $this->managers[$type]->expects($this->once())
        ->method('getDefinitions')
56
        ->will($this->returnValue(['aggregator_test' => ['title' => '', 'description' => '']]));
57 58
    }

59 60 61 62
    /** @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockBuilder $messenger */
    $messenger = $this->createMock(MessengerInterface::class);
    $messenger->expects($this->any())->method('addMessage');

63 64 65 66 67 68 69
    $this->settingsForm = new SettingsForm(
      $this->configFactory,
      $this->managers['fetcher'],
      $this->managers['parser'],
      $this->managers['processor'],
      $this->getStringTranslationStub()
    );
70
    $this->settingsForm->setMessenger($messenger);
71 72 73 74 75 76 77 78 79
  }

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

    $test_processor = $this->getMock(
      'Drupal\aggregator_test\Plugin\aggregator\processor\TestProcessor',
88 89
      ['buildConfigurationForm', 'validateConfigurationForm', 'submitConfigurationForm'],
      [[], 'aggregator_test', ['description' => ''], $this->configFactory]
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    );
    $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));

107
    $form = $this->settingsForm->buildForm([], $form_state);
108 109 110 111 112
    $this->settingsForm->validateForm($form, $form_state);
    $this->settingsForm->submitForm($form, $form_state);
  }

}