e92685b845efccaf036a1803ae9f856347ef70e0
[yaffs-website] / web / core / modules / image / tests / src / Kernel / Migrate / d6 / MigrateImageCacheTest.php
1 <?php
2
3 namespace Drupal\Tests\image\Kernel\Migrate\d6;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\image\Entity\ImageStyle;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\migrate\Exception\RequirementsException;
9 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
10
11 /**
12  * Tests migration of ImageCache presets to image styles.
13  *
14  * @group image
15  */
16 class MigrateImageCacheTest extends MigrateDrupal6TestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setUp() {
22     parent::setUp();
23     $this->installConfig(['image']);
24   }
25
26   /**
27    * Tests that an exception is thrown when ImageCache is not installed.
28    */
29   public function testMissingTable() {
30     $this->sourceDatabase->update('system')
31       ->fields([
32         'status' => 0,
33       ])
34       ->condition('name', 'imagecache')
35       ->condition('type', 'module')
36       ->execute();
37
38     try {
39       $this->getMigration('d6_imagecache_presets')
40         ->getSourcePlugin()
41         ->checkRequirements();
42       $this->fail('Did not catch expected RequirementsException.');
43     }
44     catch (RequirementsException $e) {
45       $this->pass('Caught expected RequirementsException: ' . $e->getMessage());
46     }
47   }
48
49   /**
50    * Test basic passing migrations.
51    */
52   public function testPassingMigration() {
53     $this->executeMigration('d6_imagecache_presets');
54
55     /** @var \Drupal\image\Entity\ImageStyle $style */
56     $style = ImageStyle::load('big_blue_cheese');
57
58     // Check basic Style info.
59     $this->assertIdentical('big_blue_cheese', $style->get('name'), 'ImageStyle name set correctly');
60     $this->assertIdentical('big_blue_cheese', $style->get('label'), 'ImageStyle label set correctly');
61
62     // Test effects.
63     $effects = $style->getEffects();
64
65     // Check crop effect.
66     $this->assertImageEffect($effects, 'image_crop', [
67       'width' => 555,
68       'height' => 5555,
69       'anchor' => 'center-center',
70     ]);
71
72     // Check resize effect.
73     $this->assertImageEffect($effects, 'image_resize', [
74       'width' => 55,
75       'height' => 55,
76     ]);
77
78     // Check rotate effect.
79     $this->assertImageEffect($effects, 'image_rotate', [
80       'degrees' => 55,
81       'random' => FALSE,
82       'bgcolor' => '',
83     ]);
84   }
85
86   /**
87    * Test that missing actions causes failures.
88    */
89   public function testMissingEffectPlugin() {
90     Database::getConnection('default', 'migrate')->insert("imagecache_action")
91       ->fields([
92        'presetid',
93        'weight',
94        'module',
95        'action',
96        'data',
97      ])
98       ->values([
99        'presetid' => '1',
100        'weight' => '0',
101        'module' => 'imagecache',
102        'action' => 'imagecache_deprecated_scale',
103        'data' => 'a:3:{s:3:"fit";s:7:"outside";s:5:"width";s:3:"200";s:6:"height";s:3:"200";}',
104      ])->execute();
105
106     $this->startCollectingMessages();
107     $this->executeMigration('d6_imagecache_presets');
108     $messages = $this->migration->getIdMap()->getMessageIterator();
109     $count = 0;
110     foreach ($messages as $message) {
111       $count++;
112       $this->assertEqual($message->message, 'The "image_deprecated_scale" plugin does not exist.');
113       $this->assertEqual($message->level, MigrationInterface::MESSAGE_ERROR);
114     }
115     // There should be only the one message.
116     $this->assertEqual($count, 1);
117   }
118
119   /**
120    * Test that missing action's causes failures.
121    */
122   public function testInvalidCropValues() {
123     Database::getConnection('default', 'migrate')->insert("imagecache_action")
124       ->fields([
125        'presetid',
126        'weight',
127        'module',
128        'action',
129        'data',
130      ])
131       ->values([
132        'presetid' => '1',
133        'weight' => '0',
134        'module' => 'imagecache',
135        'action' => 'imagecache_crop',
136        'data' => serialize([
137          'xoffset' => '10',
138          'yoffset' => '10',
139        ]),
140      ])->execute();
141
142     $this->startCollectingMessages();
143     $this->executeMigration('d6_imagecache_presets');
144     $this->assertEqual([
145       'error' => [
146         'The Drupal 8 image crop effect does not support numeric values for x and y offsets. Use keywords to set crop effect offsets instead.',
147       ],
148     ], $this->migrateMessages);
149   }
150
151   /**
152    * Assert that a given image effect is migrated.
153    *
154    * @param array $collection
155    *   Collection of effects
156    * @param $id
157    *   Id that should exist in the collection.
158    * @param $config
159    *   Expected configuration for the collection.
160    *
161    * @return bool
162    */
163   protected function assertImageEffect($collection, $id, $config) {
164     /** @var \Drupal\image\ConfigurableImageEffectBase $effect */
165     foreach ($collection as $key => $effect) {
166       $effect_config = $effect->getConfiguration();
167
168       if ($effect_config['id'] == $id && $effect_config['data'] == $config) {
169         // We found this effect so succeed and return.
170         return $this->pass('Effect ' . $id . ' imported correctly');
171       }
172     }
173     // The loop did not find the effect so we it was not imported correctly.
174     return $this->fail('Effect ' . $id . ' did not import correctly');
175   }
176
177 }