/**
  * Tests the active class on the link method.
  *
  * @see \Drupal\Core\Utility\LinkGenerator::generate()
  */
 public function testGenerateActive()
 {
     $this->urlGenerator->expects($this->exactly(5))->method('generateFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), FALSE, '/test-route-1'), array('test_route_3', array(), FALSE, '/test-route-3'), array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'))));
     $this->urlGenerator->expects($this->exactly(4))->method('getPathFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), 'test-route-1'), array('test_route_3', array(), 'test-route-3'), array('test_route_4', array('object' => '1'), 'test-route-4/1'))));
     $this->moduleHandler->expects($this->exactly(5))->method('alter');
     // Render a link.
     $url = new Url('test_route_1', array(), array('set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the set_active_class option disabled.
     $url = new Url('test_route_1', array(), array('set_active_class' => FALSE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', $result);
     // Render a link with an associated language.
     $url = new Url('test_route_1', array(), array('language' => new Language(array('id' => 'de')), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-1', 'hreflang' => 'de')), $result);
     // Render a link with a query parameter.
     $url = new Url('test_route_3', array(), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => '{"value":"example_1"}')), $result);
     // Render a link with route parameters and a query parameter.
     $url = new Url('test_route_4', array('object' => '1'), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-4/1', 'data-drupal-link-query' => '{"value":"example_1"}')), $result);
 }
 /**
  * Tests the active class on the link method.
  *
  * @see \Drupal\Core\Utility\LinkGenerator::generate()
  *
  * @todo Test that the active class is added on the front page when generating
  *   links to the front page when drupal_is_front_page() is converted to a
  *   service.
  */
 public function testGenerateActive()
 {
     $this->urlGenerator->expects($this->exactly(8))->method('generateFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), FALSE, '/test-route-1'), array('test_route_3', array(), FALSE, '/test-route-3'), array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'))));
     $this->urlGenerator->expects($this->exactly(7))->method('getPathFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), 'test-route-1'), array('test_route_3', array(), 'test-route-3'), array('test_route_4', array('object' => '1'), 'test-route-4/1'))));
     $this->moduleHandler->expects($this->exactly(8))->method('alter');
     // Render a link with a path different from the current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the same path as the current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the same path as the current path, but with the
     // set_active_class option disabled.
     $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => FALSE));
     $this->assertNotTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the same path and language as the current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the same path but a different language than the current
     // path.
     $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('language' => new Language(array('id' => 'de')), 'set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1', 'hreflang' => 'de')), $result);
     // Render a link with the same path and query parameter as the current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_3', array(), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/')), $result);
     // Render a link with the same path but a different query parameter than the
     // current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_3', array(), array('query' => array('value' => 'example_2'), 'set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => 'regexp:/.*value.*example_2.*/')), $result);
     // Render a link with the same path and query parameter as the current path.
     $result = $this->linkGenerator->generate('Test', 'test_route_4', array('object' => '1'), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $this->assertTag(array('tag' => 'a', 'attributes' => array('data-drupal-link-system-path' => 'test-route-4/1', 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/')), $result);
 }
Example #3
0
 /**
  * Tests altering the URL object using hook_link_alter().
  *
  * @covers ::generate
  */
 public function testGenerateWithAlterHook()
 {
     $options = ['query' => [], 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE];
     $this->urlGenerator->expects($this->any())->method('generateFromRoute')->will($this->returnValueMap([['test_route_1', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-1')], ['test_route_2', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-2')]]));
     $url = new Url('test_route_2');
     $url->setUrlGenerator($this->urlGenerator);
     $this->moduleHandler->expects($this->atLeastOnce())->method('alter')->willReturnCallback(function ($hook, &$options) {
         $options['url'] = (new Url('test_route_1'))->setUrlGenerator($this->urlGenerator);
     });
     $expected_link_markup = '<a href="/test-route-1">Test</a>';
     $this->assertEquals($expected_link_markup, (string) $this->linkGenerator->generate('Test', $url)->getGeneratedLink());
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $default_queue = $this->state_interface->get('queue_default');
     $config = $this->config('aws_sqs.settings');
     $aws_credentials_url = Url::fromUri('http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html');
     $form['credentials'] = array('#type' => 'fieldset', '#title' => t('AWS credentials'), '#description' => t('Follow the instructions to set up your AWS credentials !here.', array('!here' => $this->link_generator->generate(t('here'), $aws_credentials_url))));
     $form['credentials']['aws_sqs_aws_key'] = array('#type' => 'textfield', '#title' => t('Access Key ID'), '#default_value' => $config->get('aws_sqs_aws_key'), '#required' => TRUE, '#description' => t('Amazon Web Services Key.'));
     $form['credentials']['aws_sqs_aws_secret'] = array('#type' => 'textfield', '#title' => t('Secret Access Key'), '#default_value' => $config->get('aws_sqs_aws_secret'), '#required' => TRUE, '#description' => t('Amazon Web Services Secret Key.'));
     $long_polling_url = Url::fromUri('http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html#sqs-long-polling-query-api');
     $seconds = range(0, 20);
     $t_args = array('!more' => $this->link_generator->generate(t('Read more about long polling here.'), $long_polling_url));
     $form['aws_sqs_waittimeseconds'] = array('#type' => 'select', '#title' => t('Wait Time'), '#default_value' => $config->get('aws_sqs_waittimeseconds'), '#options' => $seconds, '#description' => t("How long do you want to stay connected to AWS waiting for a response (seconds)? If a queue\n        is empty, the connection will stay open for up to 20 seconds. If something arrives in the queue, it is\n        returned as soon as it is received. AWS SQS charges per request. Long connections that stay open waiting for\n        data to arrive are cheaper than polling SQS constantly to check for data. Long polling can also consume more\n        resources on your server (think about the difference between running a task every minute that takes a second\n        to complete versus running a task every minute that stays connected for up to 20 seconds every time waiting for\n        jobs to come in). !more", $t_args));
     $visibility_timeout_url = Url::fromUri('http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html');
     $t_args = array('!more' => $this->link_generator->generate(t('Read more about visibility timeouts here.'), $visibility_timeout_url));
     $form['aws_sqs_claimtimeout'] = array('#type' => 'textfield', '#title' => t("Claim Timeout / Visibility Timeout"), '#default_value' => $config->get('aws_sqs_claimtimeout'), '#size' => 15, '#description' => t("When an item is claimed from the queue by a worker, how long should the item be hidden from\n        other workers (seconds)? Note: If the item is not deleted before the end of this time, it will become visible\n        to other workers and available to be claimed again. Note also: 12 hours (43,200 seconds) is the maximum amount\n        of time for which an item can be claimed. !more", $t_args));
     $form['aws_sqs_region'] = array('#type' => 'select', '#title' => t('AWS Queue Region'), '#default_value' => $config->get('aws_sqs_region'), '#options' => array('us-east-1' => 'us-east-1', 'us-west-1' => 'us-west-1', 'us-west-2' => 'us-west-2', 'eu-west-1' => 'eu-west-1', 'ap-southeast-1' => 'ap-southeast-1', 'ap-northeast-1' => 'ap-northeast-1', 'sa-east-1' => 'sa-east-1'), '#required' => TRUE, '#description' => t('AWS Region where to store the Queue.'));
     $form['version'] = array('#type' => 'textfield', '#title' => t('Version'), '#default_value' => $config->get('aws_sqs_version'), '#required' => TRUE, '#description' => t("Amazon Web Services Version. 'latest' recommended"));
     if (!$default_queue) {
         $default_queue = 'queue.database';
     }
     $form['queue_default_class'] = array('#title' => t('Default Queue'), '#markup' => t("The default queue class is <strong>!default_queue</strong>.", array('!default_queue' => $default_queue)));
     return parent::buildForm($form, $form_state);
 }
 /**
  * Tests the LinkGenerator's support for collecting cacheability metadata.
  *
  * @see \Drupal\Core\Utility\LinkGenerator::generate()
  * @see \Drupal\Core\Utility\LinkGenerator::generateFromLink()
  */
 public function testGenerateCacheability()
 {
     $options = ['query' => [], 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE];
     $this->urlGenerator->expects($this->any())->method('generateFromRoute')->will($this->returnValueMap([['test_route_1', [], $options, FALSE, '/test-route-1'], ['test_route_1', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-1')]]));
     $url = new Url('test_route_1');
     $url->setUrlGenerator($this->urlGenerator);
     $expected_link_markup = '<a href="/test-route-1">Test</a>';
     // Test ::generate().
     $this->assertSame($expected_link_markup, $this->linkGenerator->generate('Test', $url));
     $generated_link = $this->linkGenerator->generate('Test', $url, TRUE);
     $this->assertSame($expected_link_markup, $generated_link->getGeneratedLink());
     $this->assertInstanceOf('\\Drupal\\Core\\Cache\\CacheableMetadata', $generated_link);
     // Test ::generateFromLink().
     $link = new Link('Test', $url);
     $this->assertSame($expected_link_markup, $this->linkGenerator->generateFromLink($link));
     $generated_link = $this->linkGenerator->generateFromLink($link, TRUE);
     $this->assertSame($expected_link_markup, $generated_link->getGeneratedLink());
     $this->assertInstanceOf('\\Drupal\\Core\\Cache\\CacheableMetadata', $generated_link);
 }