diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php new file mode 100644 index 0000000..d4dcf52 --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php @@ -0,0 +1,31 @@ + 'Aggregator plugin settings tests', + 'description' => 'Test settings configuration of individual aggregator plugins.', + 'group' => 'Aggregator', + ); + } + + public function setUp() { + $this->configFactory = $this->getConfigFactoryStub( + array( + 'aggregator.settings' => array( + 'processors' => array('aggregator_test'), + ), + 'aggregator_test.settings' => array(), + ) + ); + foreach (array('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(array('aggregator_test' => array('title' => '', 'description' => '')))); + } + + $this->settingsForm = new SettingsForm( + $this->configFactory, + $this->getStringTranslationStub(), + $this->managers['fetcher'], + $this->managers['parser'], + $this->managers['processor'] + ); + } + + /** + * Test for AggregatorPluginSettingsBase. + * + * Ensure that the settings form calls build, validate and submit methods on + * plugins that extend AggregatorPluginSettingsBase. + */ + public function testSettingsForm() { + $form_state = array(); + $test_processor = $this->getMock( + 'Drupal\aggregator_test\Plugin\aggregator\processor\TestProcessor', + array('buildForm', 'validateForm', 'submitForm'), + array(array(), 'aggregator_test', array(), $this->configFactory) + ); + $test_processor->expects($this->at(0)) + ->method('buildForm') + ->with($this->anything(), $form_state) + ->will($this->returnArgument(0)); + $test_processor->expects($this->at(1)) + ->method('validateForm') + ->with($this->anything(), $form_state); + // @todo Restore when we get rid of drupal_set_message(). + /*$test_processor->expects($this->at(2)) + ->method('submitForm') + ->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(array(), $form_state); + $this->settingsForm->validateForm($form, $form_state); + // @todo Restore when we get rid of drupal_set_message(). + //$this->settingsForm->submitForm($form, $form_state); + } + +} diff --git a/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.info.yml b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.info.yml new file mode 100644 index 0000000..835d3ed --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.info.yml @@ -0,0 +1,7 @@ +name: 'Aggregator module tests' +type: module +description: 'Support module for aggregator related testing.' +package: Testing +version: VERSION +core: 8.x +hidden: true diff --git a/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.module b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.module new file mode 100644 index 0000000..b18f87b --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.module @@ -0,0 +1,74 @@ + 'Test feed static last modified date', + 'description' => "A cached test feed with a static last modified date.", + 'page callback' => 'aggregator_test_feed', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + $items['aggregator/redirect'] = array( + 'title' => 'Test feed with a redirect', + 'description' => "A feed that redirects to another one", + 'page callback' => 'aggregator_test_redirect', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; +} + +/** + * Page callback. Generates a test feed and simulates last-modified and etags. + * + * @param $use_last_modified + * Set TRUE to send a last modified header. + * @param $use_etag + * Set TRUE to send an etag. + */ +function aggregator_test_feed($use_last_modified = FALSE, $use_etag = FALSE) { + $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT'); + $etag = Crypt::hashBase64($last_modified); + + $if_modified_since = strtotime(Drupal::request()->server->get('HTTP_IF_MODIFIED_SINCE')); + $if_none_match = stripslashes(Drupal::request()->server->get('HTTP_IF_NONE_MATCH')); + + // Send appropriate response. We respond with a 304 not modified on either + // etag or on last modified. + if ($use_last_modified) { + drupal_add_http_header('Last-Modified', gmdate(DATE_RFC1123, $last_modified)); + } + if ($use_etag) { + drupal_add_http_header('ETag', $etag); + } + // Return 304 not modified if either last modified or etag match. + if ($last_modified == $if_modified_since || $etag == $if_none_match) { + drupal_add_http_header('Status', '304 Not Modified'); + return; + } + + // The following headers force validation of cache: + drupal_add_http_header('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT'); + drupal_add_http_header('Cache-Control', 'must-revalidate'); + drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8'); + + // Read actual feed from file. + $file_name = __DIR__ . '/aggregator_test_rss091.xml'; + $handle = fopen($file_name, 'r'); + $feed = fread($handle, filesize($file_name)); + fclose($handle); + + print $feed; +} + +/** + * Page callback that redirects to another feed. + */ +function aggregator_test_redirect() { + drupal_goto('aggregator/test-feed', array(), 301); +} diff --git a/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_atom.xml b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_atom.xml new file mode 100644 index 0000000..357b2e5 --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_atom.xml @@ -0,0 +1,20 @@ + + + + Example Feed + + 2003-12-13T18:30:02Z + + John Doe + + urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6 + + + Atom-Powered Robots Run Amok + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + + diff --git a/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_rss091.xml b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_rss091.xml new file mode 100644 index 0000000..2944022 --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_rss091.xml @@ -0,0 +1,41 @@ + + + + Example + http://example.com + Example updates + en-us + Copyright 2000, Example team. + editor@example.com + webmaster@example.com + + Example + http://example.com/images/druplicon.png + http://example.com + 88 + 100 + Example updates + + + First example feed item title + http://example.com/example-turns-one + First example feed item description. + + + Second example feed item title. This title is extremely long so that it exceeds the 255 character limit for titles in feed item storage. In fact it's so long that this sentence isn't long enough so I'm rambling a bit to make it longer, nearly there now. Ah now it's long enough so I'll shut up. + http://example.com/example-turns-two + Second example feed item description. + + + Long link feed item title. + http://example.com/tomorrow/and/tomorrow/and/tomorrow/creeps/in/this/petty/pace/from/day/to/day/to/the/last/syllable/of/recorded/time/and/all/our/yesterdays/have/lighted/fools/the/way/to/dusty/death/out/out/brief/candle/life/is/but/a/walking/shadow/a/poor/player/that/struts/and/frets/his/hour/upon/the/stage/and/is/heard/no/more/it/is/a/tale/told/by/an/idiot/full/of/sound/and/fury/signifying/nothing + Long link feed item description. + + + Long author feed item title. + http://example.com/long/author + I wanted to get out and walk eastward toward the park through the soft twilight, but each time I tried to go I became entangled in some wild, strident argument which pulled me back, as if with ropes, into my chair. Yet high over the city our line of yellow windows must have contributed their share of human secrecy to the casual watcher in the darkening streets, and I was him too, looking up and wondering. I was within and without, simultaneously enchanted and repelled by the inexhaustible variety of life. + Long author feed item description. + + + diff --git a/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_title_entities.xml b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_title_entities.xml new file mode 100644 index 0000000..e526e44 --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/aggregator_test_title_entities.xml @@ -0,0 +1,14 @@ + + + + Example with Entities + http://example.com + Example RSS Feed With HTML Entities in Title + en-us + + Quote" Amp& + http://example.com/example-turns-one + Some text. + + + diff --git a/core/modules/aggregator/tests/modules/aggregator_test/config/aggregator_test.settings.yml b/core/modules/aggregator/tests/modules/aggregator_test/config/aggregator_test.settings.yml new file mode 100644 index 0000000..f858e50 --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/config/aggregator_test.settings.yml @@ -0,0 +1,2 @@ +items: + dummy_length: 5 diff --git a/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/fetcher/TestFetcher.php b/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/fetcher/TestFetcher.php new file mode 100644 index 0000000..8e69f8d --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/fetcher/TestFetcher.php @@ -0,0 +1,39 @@ +label() == 'Do not fetch') { + return FALSE; + } + return parent::fetch($feed); + } +} diff --git a/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/parser/TestParser.php b/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/parser/TestParser.php new file mode 100644 index 0000000..a9ab57c --- /dev/null +++ b/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/parser/TestParser.php @@ -0,0 +1,37 @@ +get('config.factory')); + } + + /** + * Constructs a DefaultProcessor object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Config\ConfigFactory $config + * The configuration factory object. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactory $config) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->configFactory = $config; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { + $processors = $this->configFactory->get('aggregator.settings')->get('processors'); + $info = $this->getDefinition(); + + $form['processors'][$info['id']] = array( + '#type' => 'details', + '#title' => t('Test processor settings'), + '#description' => $info['description'], + '#collapsed' => !in_array($info['id'], $processors), + ); + // Add some dummy settings to verify settingsForm is called. + $form['processors'][$info['id']]['dummy_length'] = array( + '#title' => t('Dummy length setting'), + '#type' => 'number', + '#min' => 1, + '#max' => 1000, + '#default_value' => $this->configFactory->get('aggregator_test.settings')->get('items.dummy_length'), + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $this->configFactory->get('aggregator_test.settings') + ->set('items.dummy_length', $form_state['values']['dummy_length']) + ->save(); + } + + /** + * {@inheritdoc} + */ + public function process(Feed $feed) { + foreach ($feed->items as &$item) { + // Prepend our test string. + $item['title'] = 'testProcessor' . $item['title']; + } + } + + /** + * {@inheritdoc} + */ + public function remove(Feed $feed) { + // Append a random number, just to change the feed description. + $feed->description->value .= rand(0, 10); + } + + /** + * {@inheritdoc} + */ + public function postProcess(Feed $feed) { + // Double the refresh rate. + $feed->refresh->value *= 2; + $feed->save(); + } + +}