You are here

TranslationProgressCalculatorTest.php in TMGMT Translator Smartling 8.3

File

tests/src/Kernel/TranslationProgressCalculatorTest.php
View source
<?php

namespace Drupal\Tests\tmgmt_smartling\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tmgmt_smartling\Smartling\TranslationProgressCalculator;

/**
 * Tests for progress calculator.
 *
 * @group tmgmt_smartling
 */
class TranslationProgressCalculatorTest extends KernelTestBase {

  /**
   * Tests calculate.
   *
   * @param $auto_authorize
   * @param $test_data
   * @param $logger_called_times
   * @param $result
   *
   * @throws \Exception
   * @dataProvider providerTestCalculate
   */
  public function testCalculate($auto_authorize, $test_data, $logger_called_times, $result) {
    $logger_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannel')
      ->setMethods([
      "warning",
    ])
      ->disableOriginalConstructor()
      ->getMock();
    $logger_mock
      ->expects($this
      ->exactly($logger_called_times))
      ->method("warning")
      ->with(t("Translation progress in dashboard 100% but for the connector progress = @percentage%.", [
      "@percentage" => $result,
    ]));
    $calculator = new TranslationProgressCalculator($logger_mock);
    $this
      ->assertEquals($calculator
      ->calculate($test_data, $auto_authorize), $result);
  }

  /**
   * Tests calculate: invalid data.
   *
   * @expectedException \InvalidArgumentException
   * @expectedExceptionMessage Invalid input data: {"totalStringCount":0}
   */
  public function testCalculateInvalidData() {
    $logger_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannel')
      ->setMethods(NULL)
      ->disableOriginalConstructor()
      ->getMock();
    $calculator = new TranslationProgressCalculator($logger_mock);
    $calculator
      ->calculate([
      "totalStringCount" => 0,
    ]);
  }

  /**
   * Tests validate.
   *
   * @param $test_data
   * @param $result
   *
   * @dataProvider providerTestValidate
   */
  public function testValidate($test_data, $result) {
    $logger_mock = $this
      ->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannel')
      ->setMethods(NULL)
      ->disableOriginalConstructor()
      ->getMock();
    $calculator = new TranslationProgressCalculator($logger_mock);
    $this
      ->assertEquals($calculator
      ->isValid($test_data), $result);
  }

  /**
   * Data provider for self::testCalculate().
   */
  public function providerTestCalculate() {

    // The formula was tested on following set and was compared with dashboard.
    // Progress = 0%, 4 strings are ready for translation but translator
    // didn't start yet.
    $cases[] = [
      "auto_authorize" => TRUE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 4,
        "completedStringCount" => 0,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 0,
      "result" => 0,
    ];

    // Progress = 55%, it's common case.
    $cases[] = [
      "auto_authorize" => TRUE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 4,
        "completedStringCount" => 5,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 0,
      "result" => 55,
    ];

    // Progress = 100%, looks like only 5 strings were authorized. There can
    // be a problem that user could forget about 4 strings and we will not let
    // him know. As a result, the file will be downloaded with 5 translated and
    // 4 original strings.
    $cases[] = [
      "auto_authorize" => TRUE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 5,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 0,
      "result" => 100,
    ];

    // Progress = 0%, file was uploaded without authorization. User will do
    // this later or will add file to job.
    $cases[] = [
      "auto_authorize" => TRUE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "logger_called" => 0,
      "result" => 0,
    ];

    // Progress = 100%, nothing to translate, user excluded all content.
    $cases[] = [
      "auto_authorize" => TRUE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 10,
      ],
      "logger_called" => 0,
      "result" => 100,
    ];

    // The new formula which takes unauthorized strings into account.
    // The translation progress will be different than on "Files" tab in
    // dashboard. Progress = 55%, it's common case.
    $cases[] = [
      "auto_authorize" => FALSE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 4,
        "completedStringCount" => 5,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 0,
      "result" => 55,
    ];

    // Progress = 55%, 2 strings are still in progress but user also has 2
    // unauthorized strings which we also use in formula.
    $cases[] = [
      "auto_authorize" => FALSE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 2,
        "completedStringCount" => 5,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 1,
      "result" => 55,
    ];

    // Progress = 55%, 5 strings were authorized and already translated.
    // Dashboard shows 100% but we still treat file as untranslated.
    $cases[] = [
      "auto_authorize" => FALSE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 5,
        "excludedStringCount" => 1,
      ],
      "logger_called" => 1,
      "result" => 55,
    ];

    // Progress = 0%, file was uploaded without authorization. User will do
    // this later or will add file to job.
    $cases[] = [
      "auto_authorize" => FALSE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "logger_called" => 1,
      "result" => 0,
    ];

    // Progress = 100%, nothing to translate, user excluded all content.
    $cases[] = [
      "auto_authorize" => FALSE,
      "data" => [
        "totalStringCount" => 10,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 10,
      ],
      "logger_called" => 0,
      "result" => 100,
    ];
    return $cases;
  }

  /**
   * Data provider for self::testValidate().
   */
  public function providerTestValidate() {
    $cases[] = [
      "data" => [
        "totalStringCount" => 0,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "result" => TRUE,
    ];
    $cases[] = [
      "data" => [
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "result" => FALSE,
    ];
    $cases[] = [
      "data" => [
        "totalStringCount" => 0,
        "completedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "result" => FALSE,
    ];
    $cases[] = [
      "data" => [
        "totalStringCount" => 0,
        "authorizedStringCount" => 0,
        "excludedStringCount" => 0,
      ],
      "result" => FALSE,
    ];
    $cases[] = [
      "data" => [
        "totalStringCount" => 0,
        "authorizedStringCount" => 0,
        "completedStringCount" => 0,
      ],
      "result" => FALSE,
    ];
    return $cases;
  }

}

Classes

Namesort descending Description
TranslationProgressCalculatorTest Tests for progress calculator.