/**
  * @covers ::getLabel
  */
 public function testGetLabel()
 {
     $this->pluginDefinition['label'] = 'Test label';
     $this->stringTranslation->expects($this->once())->method('translate')->with($this->pluginDefinition['label'], array(), array('context' => 'breakpoint'))->will($this->returnValue('Test label translated'));
     $this->setupBreakpoint();
     $this->assertEquals('Test label translated', $this->breakpoint->getLabel());
 }
 /**
  * @covers ::buildSelector
  */
 public function testBuildSelector()
 {
     $this->stringTranslation->expects($this->any())->method('translate')->willReturnArgument(0);
     $method = new \ReflectionMethod($this->sut, 'buildSelector');
     $method->setAccessible(TRUE);
     $plugin_id = $this->randomMachineName();
     $plugin_label = $this->randomMachineName();
     $plugin_definition = $this->getMock(PluginLabelDefinitionInterface::class);
     $plugin_definition->expects($this->atLeastOnce())->method('getLabel')->willReturn($plugin_label);
     $plugin = $this->getMock(PluginInspectionInterface::class);
     $plugin->expects($this->atLeastOnce())->method('getPluginDefinition')->willReturn($plugin_definition);
     $plugin->expects($this->atLeastOnce())->method('getPluginId')->willReturn($plugin_id);
     $this->selectablePluginType->expects($this->atLeastOnce())->method('ensureTypedPluginDefinition')->willReturnArgument(0);
     $this->sut->setSelectedPlugin($plugin);
     $selector_title = $this->randomMachineName();
     $this->sut->setLabel($selector_title);
     $selector_description = $this->randomMachineName();
     $this->sut->setDescription($selector_description);
     $element = array('#parents' => array('foo', 'bar'), '#title' => $selector_title);
     $form_state = $this->getMock(FormStateInterface::class);
     $available_plugins = array($plugin);
     $expected_build_plugin_id = array('#ajax' => array('callback' => array(Radios::class, 'ajaxRebuildForm'), 'effect' => 'fade', 'event' => 'change', 'progress' => 'none', 'trigger_as' => array('name' => 'foo[bar][select][container][change]')), '#attached' => ['library' => ['plugin/plugin_selector.plugin_radios']], '#default_value' => $plugin_id, '#empty_value' => 'select', '#options' => array($plugin_id => $plugin_label), '#required' => FALSE, '#title' => $selector_title, '#description' => $selector_description, '#type' => 'radios');
     $expected_build_change = array('#ajax' => array('callback' => array(AdvancedPluginSelectorBase::class, 'ajaxRebuildForm')), '#attributes' => array('class' => array('js-hide')), '#limit_validation_errors' => array(array('foo', 'bar', 'select', 'plugin_id')), '#name' => 'foo[bar][select][container][change]', '#submit' => [[AdvancedPluginSelectorBase::class, 'rebuildForm']], '#type' => 'submit', '#value' => 'Choose');
     $build = $method->invokeArgs($this->sut, array($element, $form_state, $available_plugins));
     $this->assertEquals($expected_build_plugin_id, $build['container']['plugin_id']);
     $this->assertEquals($expected_build_change, $build['container']['change']);
     $this->assertSame('container', $build['container']['#type']);
 }
 /**
  * {@inheritdoc}
  */
 public function collect(Request $request, Response $response, \Exception $exception = NULL)
 {
     if ($this->translation instanceof TranslationManagerWrapper) {
         /** \Drupal\webprofiler\StringTranslation\TranslationManagerWrapper $this->translation */
         $this->data['translations']['translated'] = $this->translation->getTranslated();
         $this->data['translations']['untranslated'] = $this->translation->getUntranslated();
     }
     $data['user_interface_translations_path'] = $this->urlGenerator->generateFromRoute('locale.translate_page');
 }
Example #4
0
 /**
  * Tests a static title with a parameter.
  *
  * @see \Drupal\Core\Controller\TitleResolver::getTitle()
  *
  * @dataProvider providerTestStaticTitleWithParameter
  */
 public function testStaticTitleWithParameter($title, $expected_title)
 {
     $raw_variables = new ParameterBag(array('test' => 'value', 'test2' => 'value2'));
     $request = new Request();
     $request->attributes->set('_raw_variables', $raw_variables);
     $route = new Route('/test-route', array('_title' => $title));
     $this->translationManager->expects($this->once())->method('translate')->with($title, $this->logicalOr($this->arrayHasKey('@test'), $this->arrayHasKey('%test'), $this->arrayHasKey('!test')), array())->will($this->returnValue('static title value'));
     $this->assertEquals($expected_title, $this->titleResolver->getTitle($request, $route));
 }
 /**
  * @covers ::getTitle
  */
 public function testGetTitleWithTitleArguments()
 {
     $title = 'Example @test';
     $this->pluginDefinition['title'] = new TranslatableMarkup($title, array('@test' => 'value'), [], $this->stringTranslation);
     $this->stringTranslation->expects($this->once())->method('translateString')->with($this->pluginDefinition['title'])->will($this->returnValue('Example value'));
     $this->setupContextualLinkDefault();
     $request = new Request();
     $this->assertEquals('Example value', $this->contextualLinkDefault->getTitle($request));
 }
Example #6
0
 /**
  * @covers ::get
  *
  * @dataProvider providerTestGet
  */
 public function testGet(array $values, $expected)
 {
     $container = new ContainerBuilder();
     $container->set('string_translation', $this->translationManager);
     \Drupal::setContainer($container);
     $arguments = isset($values['arguments']) ? $values['arguments'] : array();
     $options = isset($values['context']) ? array('context' => $values['context']) : array();
     $this->translationManager->expects($this->once())->method('translate')->with($values['value'], $arguments, $options);
     $annotation = new Translation($values);
     $this->assertSame($expected, (string) $annotation->get());
 }
 /**
  * Prepares mocks for the test.
  */
 protected function setUp()
 {
     $this->commentManager = $this->getMock('\\Drupal\\comment\\CommentManagerInterface');
     $this->stringTranslation = $this->getStringTranslationStub();
     $this->moduleHandler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
     $this->currentUser = $this->getMock('\\Drupal\\Core\\Session\\AccountProxyInterface');
     $this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->commentManager, $this->moduleHandler, $this->stringTranslation);
     $this->commentManager->expects($this->any())->method('getFields')->with('node')->willReturn(array('comment' => array()));
     $this->commentManager->expects($this->any())->method('forbiddenMessage')->willReturn("Can't let you do that Dave.");
     $this->stringTranslation->expects($this->any())->method('formatPlural')->willReturnArgument(1);
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
     $this->moduleHandler->getImplementations('entity_type_build')->willReturn([]);
     $this->moduleHandler->alter('entity_type', Argument::type('array'))->willReturn(NULL);
     $this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
     $this->translationManager = $this->prophesize(TranslationInterface::class);
     $this->entityTypeManager = new TestEntityTypeManager(new \ArrayObject(), $this->moduleHandler->reveal(), $this->cacheBackend->reveal(), $this->translationManager->reveal(), $this->getClassResolverStub());
     $this->discovery = $this->prophesize(DiscoveryInterface::class);
     $this->entityTypeManager->setDiscovery($this->discovery->reveal());
 }
 /**
  * @covers ::settingsSummary
  */
 public function testSettingsSummaryWithMultipleLineItems()
 {
     $line_items_data = [['plugin_id' => $this->randomMachineName(), 'plugin_configuration' => []], ['plugin_id' => $this->randomMachineName(), 'plugin_configuration' => []]];
     $this->sut->setSetting('line_items', $line_items_data);
     $this->stringTranslation->expects($this->any())->method('formatPlural')->with(2);
     $this->sut->settingsSummary();
 }
Example #10
0
 /**
  * @covers ::getTitle
  */
 public function testGetTitleWithTitleArguments()
 {
     $this->pluginDefinition['title'] = new TranslatableMarkup('Example @test', array('@test' => 'value'), [], $this->stringTranslation);
     $this->stringTranslation->expects($this->once())->method('translateString')->with($this->pluginDefinition['title'])->will($this->returnValue('Example value'));
     $this->setupLocalTaskDefault();
     $this->assertEquals('Example value', $this->localTaskBase->getTitle());
 }
Example #11
0
 /**
  * Translates a string to the current language or to a given language.
  *
  * See the t() documentation for details.
  */
 protected static function t($string, array $args = array(), array $options = array())
 {
     if (empty(static::$translationManager)) {
         static::$translationManager = \Drupal::service('string_translation');
     }
     return static::$translationManager->translate($string, $args, $options);
 }
Example #12
0
 /**
  * Tests the getTitle method with title arguments.
  */
 public function testGetTitleWithTitleArguments()
 {
     $this->pluginDefinition['title'] = (new TranslationWrapper('Example @test', array('@test' => 'value')))->setStringTranslation($this->stringTranslation);
     $this->stringTranslation->expects($this->once())->method('translate')->with('Example @test', array('@test' => 'value'), array())->will($this->returnValue('Example value'));
     $this->setupLocalActionDefault();
     $request = new Request();
     $this->assertEquals('Example value', $this->localActionDefault->getTitle($request));
 }
 /**
  * @covers ::getTitle
  */
 public function testGetTitleWithTitleArguments()
 {
     $this->pluginDefinition['title'] = 'Example @test';
     $this->pluginDefinition['title_arguments'] = array('@test' => 'value');
     $this->stringTranslation->expects($this->once())->method('translate')->with($this->pluginDefinition['title'], $this->arrayHasKey('@test'), array())->will($this->returnValue('Example value'));
     $this->setupLocalTaskDefault();
     $request = new Request();
     $this->assertEquals('Example value', $this->localTaskBase->getTitle($request));
 }
 /**
  * Provides a label for a property by its relationship to a context.
  *
  * @param \Drupal\Core\Field\BaseFieldDefinition $property
  *   The property for which to generate a label.
  * @param \Drupal\Core\Plugin\Context\ContextInterface $context
  *   The context to which the property is related.
  * @param $context_id
  *   The context from the previous parameter's id in the contexts array.
  *
  * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  *   A label for this property for use in user interfaces.
  */
 protected function getRelatedPropertyLabel(BaseFieldDefinition $property, ContextInterface $context, $context_id)
 {
     /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
     $label = $property->getFieldStorageDefinition()->getLabel();
     $string = "@context_id: {$label->getUntranslatedString()}";
     $args = $label->getArguments();
     $args['@context_id'] = !empty($context->getContextDefinition()->getLabel()) ? $context->getContextDefinition()->getLabel() : "{$context_id}";
     $options = $label->getOptions();
     // @todo we need to really think about this label. It informs the UI extensively and should be as clear as possible.
     return $this->translation->translate($string, $args, $options);
 }
Example #15
0
 /**
  * Tests the formatDiff method.
  *
  * @dataProvider providerTestFormatDiff
  *
  * @covers ::formatDiff
  */
 public function testformatDiff($expected, $timestamp1, $timestamp2, $options = array())
 {
     // Mocks a simple formatPlural implementation.
     $this->stringTranslation->expects($this->any())->method('formatPlural')->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => isset($options['langcode']) ? $options['langcode'] : NULL))->will($this->returnCallback(function ($count, $one, $multiple) {
         return $count == 1 ? $one : str_replace('@count', $count, $multiple);
     }));
     // Mocks a simple translate implementation.
     $this->stringTranslation->expects($this->any())->method('translate')->with($this->anything())->will($this->returnCallback(function ($string, $args, $options) {
         return $string;
     }));
     $this->assertEquals($expected, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
 }
 /**
  * @covers ::buildForm
  */
 public function testBuildForm()
 {
     $form = array();
     $form_state = $this->getMock(FormStateInterface::class);
     $definitions = array('foo' => array('label' => $this->randomMachineName()));
     $plugin_id = $this->randomMachineName();
     $this->currencyAmountFormatterManager->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $config->expects($this->once())->method('get')->with('plugin_id')->willReturn($plugin_id);
     $this->configFactory->expects($this->once())->method('getEditable')->with('currency.amount_formatting')->willReturn($config);
     $this->stringTranslation->expects($this->any())->method('translate')->will($this->returnArgument(0));
     $expected = array('#default_value' => $plugin_id, '#options' => array('foo' => $definitions['foo']['label']), '#process' => [[Radios::class, 'processRadios'], [$this->controller, 'processPluginOptions']], '#type' => 'radios');
     $build = $this->controller->buildForm($form, $form_state);
     unset($build['default_plugin_id']['#title']);
     $this->assertSame($expected, $build['default_plugin_id']);
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function runCommonBuild(JuiceboxGalleryInterface $gallery, $settings, $data = NULL)
 {
     $global_settings = $this->getGlobalSettings();
     // Add all gallery options directly from the settings.
     $this->setGalleryOptions($gallery, $settings);
     // Also attempt to translate interface strings.
     if ($global_settings['translate_interface']) {
         $base_string = $global_settings['base_languagelist'];
         if (!empty($base_string)) {
             $gallery->addOption('languagelist', Html::escape($this->stringTranslation->translate($base_string)), FALSE);
         }
     }
     // Allow other modules to alter the built gallery data before it's
     // rendered. After this point the gallery should no longer change.
     $this->moduleManager->alter('juicebox_gallery', $gallery, $data);
 }
Example #18
0
 /**
  * Tests the formatDiff method.
  *
  * @dataProvider providerTestFormatDiff
  *
  * @covers ::formatDiff
  */
 public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $options = array())
 {
     // Mocks a simple translateString implementation.
     $this->stringTranslation->expects($this->any())->method('translateString')->willReturnCallback(function (TranslatableMarkup $arg) {
         return $arg->getUntranslatedString();
     });
     if (isset($options['langcode'])) {
         $expected_markup = new TranslatableMarkup($expected, [], ['langcode' => $options['langcode']], $this->stringTranslation);
     } else {
         $expected_markup = new TranslatableMarkup($expected, [], [], $this->stringTranslation);
     }
     $this->assertEquals($expected_markup, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
     $options['return_as_object'] = TRUE;
     $expected_object = new FormattedDateDiff($expected, $max_age);
     $this->assertEquals($expected_object, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
 }
Example #19
0
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $count = $this->queryFactory->get('crop')
      ->condition('type', $this->entity->id())
      ->count()
      ->execute();
    if ($count) {
      $form['#title'] = $this->getQuestion();
      $form['description'] = [
        '#prefix' => '<p>',
        '#markup' => $this->translation->formatPlural($count, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', array('%type' => $this->entity->label())),
        '#suffix' => '</p>',
      ];
      return $form;
    }

    return parent::buildForm($form, $form_state);
  }
Example #20
0
 /**
  * {@inheritdoc}
  */
 public function validate()
 {
     $errors = parent::validate();
     $pager_options = $this->displayHandler->getOption('pager');
     if (isset($pager_options['type']) && !($pager_options['type'] == 'none' || $pager_options['type'] == 'some')) {
         // @todo: Re-enable this error once issue #2579931 is resolved.
         // $errors[] = $this->stringTranslation->translate('The Juicebox style cannot be used with a pager. Please disable the "Use a pager" option for this display.');
     }
     $style = $this->displayHandler->getOption('style');
     // We want to somewhat "nag" the user if they have not yet configured the
     // Juicebox-specific plugin settings (because things won't work until they
     // do). However, we do NOT want to formally set an error. This is because
     // this validate method can run on pages where the user can't actaully touch
     // the Juicebox-specific plugin settings (such as
     // admin/structure/views/add).
     if (empty($style['options']['image_field']) || empty($style['options']['thumb_field'])) {
         drupal_set_message($this->stringTranslation->translate("To ensure a fully functional Juicebox gallery please remember to add at least one field of type Image, File or File ID to your Juicebox view display, and to configure all Juicebox Gallery format settings. Once you have completed these steps, re-save your view to remove this warning."), 'warning', FALSE);
     }
     return $errors;
 }
 /**
  * @covers ::buildSelector
  */
 public function testBuildSelector()
 {
     $this->stringTranslation->expects($this->any())->method('translate')->willReturnArgument(0);
     $method = new \ReflectionMethod($this->sut, 'buildSelector');
     $method->setAccessible(TRUE);
     $plugin_id = $this->randomMachineName();
     $plugin_label = $this->randomMachineName();
     $plugin = $this->getMock(PluginInspectionInterface::class);
     $plugin->expects($this->any())->method('getPluginId')->willReturn($plugin_id);
     $plugin->expects($this->any())->method('getPluginLabel')->willReturn($plugin_label);
     $this->sut->setSelectedPlugin($plugin);
     $element = array('#parents' => array('foo', 'bar'));
     $form_state = $this->getMock(FormStateInterface::class);
     $available_plugins = array($plugin);
     $expected_build_change = array('#ajax' => array('callback' => array(AdvancedPluginSelectorBase::class, 'ajaxRebuildForm')), '#attributes' => array('class' => array('js-hide')), '#limit_validation_errors' => array(array('foo', 'bar', 'select', 'plugin_id')), '#name' => 'foo[bar][select][container][change]', '#submit' => [[AdvancedPluginSelectorBase::class, 'rebuildForm']], '#type' => 'submit', '#value' => 'Choose');
     $build = $method->invokeArgs($this->sut, array($element, $form_state, $available_plugins));
     $this->assertArrayHasKey('plugin_id', $build['container']);
     $this->assertEquals($expected_build_change, $build['container']['change']);
     $this->assertSame('container', $build['container']['#type']);
 }
 /**
  * @covers ::execute
  * @covers ::buildListingLevel
  * @covers ::buildHierarchy
  * @covers ::buildHierarchyLevel
  * @covers ::sort
  */
 function testListing()
 {
     $plugin_id_a = $this->randomMachineName();
     $plugin_id_b = $this->randomMachineName();
     $definitions = [$plugin_id_a => ['label' => $this->randomMachineName(), 'description' => $this->randomMachineName()], $plugin_id_b => ['label' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'parent_id' => $plugin_id_a]];
     $operations_a = ['title' => $this->randomMachineName()];
     $operations_provider_a = $this->getMock(PluginOperationsProviderInterface::class);
     $operations_provider_a->expects($this->once())->method('getOperations')->with($plugin_id_a)->willReturn($operations_a);
     $map = [[$plugin_id_a, TRUE, $definitions[$plugin_id_a]], [$plugin_id_b, TRUE, $definitions[$plugin_id_b]]];
     $this->paymentStatusManager->expects($this->exactly(count($map)))->method('getDefinition')->willReturnMap($map);
     $this->paymentStatusManager->expects($this->atLeastOnce())->method('getDefinitions')->willReturn($definitions);
     $map = [[$plugin_id_a, $operations_provider_a], [$plugin_id_b, NULL]];
     $this->paymentStatusManager->expects($this->exactly(2))->method('getOperationsProvider')->willReturnMap($map);
     $this->stringTranslation->expects($this->any())->method('translate')->will($this->returnArgument(0));
     $build = $this->sut->execute();
     foreach ($build['#header'] as $key => $label) {
         $build['#header'][$key] = (string) $label;
     }
     $expected = ['#header' => ['Title', 'Description', 'Operations'], '#type' => 'table', $plugin_id_a => ['label' => ['#markup' => $definitions[$plugin_id_a]['label']], 'description' => ['#markup' => $definitions[$plugin_id_a]['description']], 'operations' => ['#type' => 'operations', '#links' => $operations_a]], $plugin_id_b => ['label' => ['#markup' => $definitions[$plugin_id_b]['label']], 'description' => ['#markup' => $definitions[$plugin_id_b]['description']], 'operations' => ['#type' => 'operations', '#links' => []]]];
     $this->assertSame($expected, $build);
 }
Example #23
0
 /**
  * Tests the formatInterval method for 0 second.
  */
 public function testFormatIntervalZeroSecond()
 {
     $this->stringTranslation->expects($this->once())->method('translate')->with('0 sec', array(), array('langcode' => 'xxx-lolspeak'))->will($this->returnValue('0 sec'));
     $result = $this->dateFormatter->formatInterval(0, 1, 'xxx-lolspeak');
     $this->assertEquals('0 sec', $result);
 }
 /**
  * Gets a translated message from the exception.
  *
  * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  *   The string translation service.
  *
  * @return string
  */
 public function getTranslatedMessage(TranslationInterface $string_translation, $extension)
 {
     return $string_translation->formatPlural(count($this->getConfigObjects()), 'Unable to install @extension, %config_names has unmet dependencies.', 'Unable to install @extension, %config_names have unmet dependencies.', ['%config_names' => implode(', ', $this->getConfigObjects()), '@extension' => $extension]);
 }
Example #25
0
 /**
  * Translates a string to the current language or to a given language.
  *
  * @see \Drupal\Core\StringTranslation\TranslationInterface()
  */
 protected function t($string, array $args = array(), array $options = array())
 {
     return $this->translation ? $this->translation->translate($string, $args, $options) : String::format($string, $args);
 }