<commit_before><?php

namespace Drupal\Tests\open_pension_files\Kernel;

use Drupal\Core\Field\Plugin\DataType\FieldItem;
use Drupal\file\Entity\File;
use Drupal\file\FileStorage;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\open_pension_files\OpenPensionFilesProcessInterface;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;

class OpenPensionFilesFileProcessTest extends KernelTestBase {

  /**
   * Modules to enable.
   */
  public static $modules = [
    'file',
    'user',
    'open_pension_files',
    'system',
    'media',
    'media_entity_browser',
    'media_library',
    'views',
    'image',
  ];

  /**
   * @var OpenPensionFilesProcessInterface
   */
  protected $openPensionFilesProcess;

  /**
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');
    $this->installEntitySchema('media');
    $this->installConfig('system');
    $logger = new OpenPensionFilesLoggerChannel('testing');

    $this->openPensionFilesProcess = $this->container->get('open_pension_files.file_process');
    $this->fileSystem = $this->container->get('file_system');

    $this->openPensionFilesProcess
      ->setHttpClient($this->getMockHttpClient())
      ->setLogger($logger);
  }

  /**
   * Get an dummy object.
   *
   * @return Client
   */
  protected function getMockHttpClient(): Client {

    $mock = new MockHandler([
      new Response(200, ['X-Foo' => 'Bar']),
      new Response(
        400,
        ['Content-Type' => 'application/json'],
        json_encode(['error' => 'cannot process xlrd files.'])
      ),
    ]);

    return new Client(['handler' => HandlerStack::create($mock)]);
  }

  /**
   * Testing the log method.
   */
  public function testLog() {
    $this->openPensionFilesProcess->log('This is a logging message', LogLevel::INFO);

    $this->assertEquals(
      $this->openPensionFilesProcess->getTrackingLogs(),
      ['This is a logging message']
    );

    $this->assertEquals(
      $this->openPensionFilesProcess->getLogger()->logs,
      [['level' => LogLevel::INFO, 'message' => 'This is a logging message']]
    );
  }

  /**
   * Testing the process files method.
   */
  public function testProcessFile() {
    // Send the file for processing.
    $this->openPensionFilesProcess->processFile(1);

    $this->assertEquals(
      $this->openPensionFilesProcess->getLogger()->logs,
      [['level' => LogLevel::INFO, 'message' => 'Could not load a file with the ID 1']]
    );

    // Replace the file service with a dummy service.
    /** @var MockObject|FileStorage $file_mock */
    $file_mock = $this
      ->getMockBuilder(FileStorage::class)
      ->disableOriginalConstructor()
      ->getMock();

    $file_mock
      ->method('load')
      ->willReturn(File::create(['bundle' => 'foo', 'filename' => 'foo']));

    // Process the file again.
    $this
      ->openPensionFilesProcess
      ->setFileStorage($file_mock)
      ->processFile(1);

    $this->assertTrue(
      $this->openPensionFilesProcess->getLogger()->logs,
      ['level' => LogLevel::INFO, 'message' => 'Starting to process the file foo']
    );

    $this->assertTrue(
      $this->openPensionFilesProcess->getLogger()->logs,
      ['level' => LogLevel::INFO, 'message' => 'The file foo has been processed']
    );

    // Now, apply the request again and look for the erros in the log.
    $this
      ->openPensionFilesProcess
      ->setFileStorage($file_mock)
      ->processFile(1);

    $error = $this->openPensionFilesProcess->getLogger()->logs;

    $message = "The file foo was not able to process due to Client error: `GET http://google.com` resulted in a `400 Bad Request` response:";
    $message .= "\n{&quot;error&quot;:&quot;cannot process xlrd files.&quot;}\n";

    $this->assertEquals($error[4]['level'], LogLevel::ERROR);
    $this->assertEquals($error[4]['message'], $message);
  }

  /**
   * Testing the update media method.
   */
  public function testUpdateEntity() {
    /** @var MockObject|FileStorage $file_mock */
    $file_mock = $this
      ->getMockBuilder(FileStorage::class)
      ->disableOriginalConstructor()
      ->getMock();

    $file_mock
      ->method('load')
      ->willReturn(File::create(['bundle' => 'foo', 'filename' => 'foo']));

    /** @var MockObject|Media $media_mock */
    $media_mock = $this
      ->getMockBuilder(Media::class)
      ->disableOriginalConstructor()
      ->getMock();

    $media_mock
      ->method('save')
      ->willReturn(TRUE);

    $field_mock = $this
      ->getMockBuilder(FieldItem::class)
      ->disableOriginalConstructor()
      ->getMock();

    $field_mock->method('appendItem')->will($this->returnSelf());

    $this
      ->openPensionFilesProcess
      ->setFileStorage($file_mock)
      ->processFile(1)
      ->updateEntity($media_mock);

    print_r($media_mock);

  }

}<commit_msg>Remove unusable test.<commit_after><?php

namespace Drupal\Tests\open_pension_files\Kernel;

use Drupal\Core\Field\Plugin\DataType\FieldItem;
use Drupal\file\Entity\File;
use Drupal\file\FileStorage;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\open_pension_files\OpenPensionFilesProcessInterface;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;

class OpenPensionFilesFileProcessTest extends KernelTestBase {

  /**
   * Modules to enable.
   */
  public static $modules = [
    'file',
    'user',
    'open_pension_files',
    'system',
    'media',
    'media_entity_browser',
    'media_library',
    'views',
    'image',
  ];

  /**
   * @var OpenPensionFilesProcessInterface
   */
  protected $openPensionFilesProcess;

  /**
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');
    $this->installEntitySchema('media');
    $this->installConfig('system');
    $logger = new OpenPensionFilesLoggerChannel('testing');

    $this->openPensionFilesProcess = $this->container->get('open_pension_files.file_process');
    $this->fileSystem = $this->container->get('file_system');

    $this->openPensionFilesProcess
      ->setHttpClient($this->getMockHttpClient())
      ->setLogger($logger);
  }

  /**
   * Get an dummy object.
   *
   * @return Client
   */
  protected function getMockHttpClient(): Client {

    $mock = new MockHandler([
      new Response(200, ['X-Foo' => 'Bar']),
      new Response(
        400,
        ['Content-Type' => 'application/json'],
        json_encode(['error' => 'cannot process xlrd files.'])
      ),
    ]);

    return new Client(['handler' => HandlerStack::create($mock)]);
  }

  /**
   * Testing the log method.
   */
  public function testLog() {
    $this->openPensionFilesProcess->log('This is a logging message', LogLevel::INFO);

    $this->assertEquals(
      $this->openPensionFilesProcess->getTrackingLogs(),
      ['This is a logging message']
    );

    $this->assertEquals(
      $this->openPensionFilesProcess->getLogger()->logs,
      [['level' => LogLevel::INFO, 'message' => 'This is a logging message']]
    );
  }

  /**
   * Testing the process files method.
   */
  public function testProcessFile() {
    // Send the file for processing.
    $this->openPensionFilesProcess->processFile(1);

    $this->assertEquals(
      $this->openPensionFilesProcess->getLogger()->logs,
      [['level' => LogLevel::INFO, 'message' => 'Could not load a file with the ID 1']]
    );

    // Replace the file service with a dummy service.
    /** @var MockObject|FileStorage $file_mock */
    $file_mock = $this
      ->getMockBuilder(FileStorage::class)
      ->disableOriginalConstructor()
      ->getMock();

    $file_mock
      ->method('load')
      ->willReturn(File::create(['bundle' => 'foo', 'filename' => 'foo']));

    // Process the file again.
    $this
      ->openPensionFilesProcess
      ->setFileStorage($file_mock)
      ->processFile(1);

    $this->assertTrue(
      $this->openPensionFilesProcess->getLogger()->logs,
      ['level' => LogLevel::INFO, 'message' => 'Starting to process the file foo']
    );

    $this->assertTrue(
      $this->openPensionFilesProcess->getLogger()->logs,
      ['level' => LogLevel::INFO, 'message' => 'The file foo has been processed']
    );

    // Now, apply the request again and look for the erros in the log.
    $this
      ->openPensionFilesProcess
      ->setFileStorage($file_mock)
      ->processFile(1);

    $error = $this->openPensionFilesProcess->getLogger()->logs;

    $message = "The file foo was not able to process due to Client error: `GET http://google.com` resulted in a `400 Bad Request` response:";
    $message .= "\n{&quot;error&quot;:&quot;cannot process xlrd files.&quot;}\n";

    $this->assertEquals($error[4]['level'], LogLevel::ERROR);
    $this->assertEquals($error[4]['message'], $message);
  }

}
{'fraction_non_alphanumeric': 0.157483684604686, 'fraction_numerical': 0.003530544559751792, 'mean_word_length': 3.353982300884956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529617', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2827, 'n_words': 577}