View source
<?php
namespace Drupal\Tests\lazyloader\Kernel;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
class ThemeTest extends KernelTestBase {
use UserCreationTrait;
public static $modules = [
'image',
'lazyloader',
'path',
'user',
'node',
'field',
'system',
'file',
'simpletest',
];
protected $node;
private $testFilesHaveBeenGenerated = FALSE;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('file');
$this
->installSchema('system', 'sequences');
$this
->installSchema('file', 'file_usage');
$this
->installSchema('node', 'node_access');
$this
->installConfig('system');
$this
->installConfig('lazyloader');
NodeType::create([
'type' => 'page',
])
->save();
$field_storage = FieldStorageConfig::create([
'type' => 'image',
'field_name' => 'field_images',
'cardinality' => -1,
'entity_type' => 'node',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => 'field_images',
'entity_type' => 'node',
'bundle' => 'page',
]);
$field
->save();
ImageStyle::create([
'name' => 'medium',
])
->save();
$display = \Drupal::service('entity_display.repository')
->getViewDisplay('node', 'page');
$display
->setComponent('field_images', [
'type' => 'image',
'settings' => [
'image_style' => 'medium',
],
]);
$display
->save();
$images = $this
->getTestFiles('image');
foreach ($images as $key => $image) {
$file = File::create((array) $image);
$file
->save();
$images[$key] = $file
->id();
}
$user = User::create([
'name' => 'muh',
]);
$user
->save();
$settings = [
'type' => 'page',
'field_images' => $images,
'path_alias' => [
'/' . $this
->randomMachineName(),
],
'title' => 'test title',
'uid' => $user
->id(),
];
$this->node = Node::create($settings);
$this->node
->save();
}
protected function getTestFiles($type, $size = NULL) {
$file_system = \Drupal::service('file_system');
if (empty($this->generatedTestFiles)) {
$lines = [
64,
1024,
];
$count = 0;
foreach ($lines as $line) {
$this
->generateFile('binary-' . $count++, 64, $line, 'binary');
}
$lines = [
16,
256,
1024,
2048,
20480,
];
$count = 0;
foreach ($lines as $line) {
$this
->generateFile('text-' . $count++, 64, $line, 'text');
}
$original = \Drupal::root() . '/core/tests/fixtures/files';
$files = $file_system
->scanDirectory($original, '/(html|image|javascript|php|sql)-.*/');
foreach ($files as $file) {
$file_system
->copy($file->uri, PublicStream::basePath());
}
$this->generatedTestFiles = TRUE;
}
$files = [];
if (in_array($type, [
'binary',
'html',
'image',
'javascript',
'php',
'sql',
'text',
])) {
$files = $file_system
->scanDirectory('public://', '/' . $type . '\\-.*/');
if ($size !== NULL) {
foreach ($files as $file) {
$stats = stat($file->uri);
if ($stats['size'] != $size) {
unset($files[$file->uri]);
}
}
}
}
usort($files, [
$this,
'compareFiles',
]);
return $files;
}
protected function compareFiles($file1, $file2) {
$compare_size = filesize($file1->uri) - filesize($file2->uri);
if ($compare_size) {
return $compare_size;
}
else {
return strnatcmp($file1->name, $file2->name);
}
}
public static function generateFile($filename, $width, $lines, $type = 'binary-text') {
$text = '';
for ($i = 0; $i < $lines; $i++) {
for ($j = 0; $j < $width - 1; $j++) {
switch ($type) {
case 'text':
$text .= chr(rand(32, 126));
break;
case 'binary':
$text .= chr(rand(0, 31));
break;
case 'binary-text':
default:
$text .= rand(0, 1);
break;
}
}
$text .= "\n";
}
$filename = 'public://' . $filename . '.txt';
file_put_contents($filename, $text);
return $filename;
}
public function testThemeLazyloaderImage() {
$request = Request::create('/');
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, '<front>');
\Drupal::requestStack()
->push($request);
$renderer = \Drupal::service('renderer');
$image = $this->node->field_images;
$path = file_url_transform_relative(file_create_url($image->entity->uri->value));
\Drupal::configFactory()
->getEditable('lazyloader.configuration')
->set('enabled', TRUE)
->save();
$render_array = [
'#uri' => $image->entity->uri->value,
'#theme' => 'image',
];
$result = $renderer
->renderPlain($render_array);
$this
->setRawContent($result);
$images = $this
->cssSelect('img');
$main_image = $images[0];
$this
->assertEquals('data:image/gif;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==', (string) $main_image['src']);
$fallback_image = $images[1];
$this
->assertEquals($path, (string) $fallback_image['src']);
}
}