|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\dgi_migrate\Unit; |
| 4 | + |
| 5 | +use Drupal\dgi_migrate\Plugin\migrate\process\TrackingGet; |
| 6 | +use Drupal\migrate\MigrateExecutableInterface; |
| 7 | +use Drupal\migrate\Plugin\MigrateProcessInterface; |
| 8 | +use Drupal\migrate\Row; |
| 9 | +use Drupal\Tests\UnitTestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test our TrackingGet class. |
| 13 | + * |
| 14 | + * @group dgi_migrate |
| 15 | + */ |
| 16 | +class TrackingGetTest extends UnitTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * Mock migration executable. |
| 20 | + * |
| 21 | + * @var \Drupal\migrate\MigrateExecutableInterface|(\Drupal\migrate\MigrateExecutableInterface&\object&\PHPUnit\Framework\MockObject\Stub)|(\Drupal\migrate\MigrateExecutableInterface&\PHPUnit\Framework\MockObject\Stub)|(\object&\PHPUnit\Framework\MockObject\Stub)|\PHPUnit\Framework\MockObject\Stub |
| 22 | + */ |
| 23 | + protected MigrateExecutableInterface $mockExecutable; |
| 24 | + |
| 25 | + /** |
| 26 | + * A randomly-generated property name for use in the given test. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected string $testPropName; |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritDoc} |
| 34 | + */ |
| 35 | + protected function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->mockExecutable = $this->createStub(MigrateExecutableInterface::class); |
| 39 | + $this->testPropName = $this->randomMachineName(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Helper; build out the plugin instance. |
| 44 | + * |
| 45 | + * @param array $configuration |
| 46 | + * Param to use during instantiation. |
| 47 | + * |
| 48 | + * @return \Drupal\dgi_migrate\Plugin\migrate\process\TrackingGet |
| 49 | + * The plugin instance. |
| 50 | + */ |
| 51 | + protected function getInstance(array $configuration) : TrackingGet { |
| 52 | + return (new TrackingGet($configuration, '', [])) |
| 53 | + ->setWrappedPlugin($this->createStub(MigrateProcessInterface::class)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test the presence of a source property is reflected. |
| 58 | + */ |
| 59 | + public function testPresentSource() : void { |
| 60 | + $row = (new Row( |
| 61 | + [ |
| 62 | + 'a' => 'a', |
| 63 | + 'b' => 'b', |
| 64 | + ], |
| 65 | + ['a' => 'a'], |
| 66 | + )) |
| 67 | + ->freezeSource(); |
| 68 | + |
| 69 | + $this->getInstance(['source' => 'b']) |
| 70 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 71 | + $row->setDestinationProperty($this->testPropName, 'b'); |
| 72 | + $this->assertTrue($row->hasDestinationProperty(TrackingGet::PROPERTY_NAME)); |
| 73 | + $this->assertTrue($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$this->testPropName]); |
| 74 | + |
| 75 | + $filtered = TrackingGet::filterRow($row); |
| 76 | + $this->assertNotContains($this->testPropName, $filtered->getEmptyDestinationProperties()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test the presence of a source property that does "skip process". |
| 81 | + */ |
| 82 | + public function testPresentEmptySourceSkip() : void { |
| 83 | + $row = (new Row( |
| 84 | + [ |
| 85 | + 'a' => 'a', |
| 86 | + 'b' => '', |
| 87 | + ], |
| 88 | + ['a' => 'a'], |
| 89 | + )) |
| 90 | + ->freezeSource(); |
| 91 | + |
| 92 | + $this->getInstance(['source' => 'b']) |
| 93 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 94 | + $row->setEmptyDestinationProperty($this->testPropName); |
| 95 | + $this->assertTrue($row->hasDestinationProperty(TrackingGet::PROPERTY_NAME)); |
| 96 | + $this->assertTrue($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$this->testPropName]); |
| 97 | + |
| 98 | + $filtered = TrackingGet::filterRow($row); |
| 99 | + $this->assertContains($this->testPropName, $filtered->getEmptyDestinationProperties()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test the presence of a source property that gets passed-through. |
| 104 | + */ |
| 105 | + public function testPresentEmptySourcePass() : void { |
| 106 | + $row = (new Row( |
| 107 | + [ |
| 108 | + 'a' => 'a', |
| 109 | + 'b' => '', |
| 110 | + ], |
| 111 | + ['a' => 'a'], |
| 112 | + )) |
| 113 | + ->freezeSource(); |
| 114 | + |
| 115 | + $this->getInstance(['source' => 'b']) |
| 116 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 117 | + $row->setDestinationProperty($this->testPropName, ''); |
| 118 | + $this->assertTrue($row->hasDestinationProperty(TrackingGet::PROPERTY_NAME)); |
| 119 | + $this->assertTrue($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$this->testPropName]); |
| 120 | + |
| 121 | + $filtered = TrackingGet::filterRow($row); |
| 122 | + $this->assertEquals('', $filtered->getDestinationProperty($this->testPropName)); |
| 123 | + $this->assertNotContains($this->testPropName, $filtered->getEmptyDestinationProperties()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Test the absence of a source property is reflected. |
| 128 | + */ |
| 129 | + public function testAbsentSource() : void { |
| 130 | + $row = (new Row( |
| 131 | + ['a' => 'a'], |
| 132 | + ['a' => 'a'], |
| 133 | + )) |
| 134 | + ->freezeSource(); |
| 135 | + |
| 136 | + $this->getInstance(['source' => 'b']) |
| 137 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 138 | + // Approximate behavior MigrateExecutable. |
| 139 | + // @see https://git.drupalcode.org/project/drupal/-/blob/10.5.x/core/modules/migrate/src/MigrateExecutable.php?ref_type=heads#L476 |
| 140 | + $row->setEmptyDestinationProperty($this->testPropName); |
| 141 | + $this->assertTrue($row->hasDestinationProperty(TrackingGet::PROPERTY_NAME)); |
| 142 | + $this->assertFalse($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$this->testPropName]); |
| 143 | + |
| 144 | + $filtered = TrackingGet::filterRow($row); |
| 145 | + $this->assertNull($filtered->getDestinationProperty($this->testPropName)); |
| 146 | + $this->assertNotContains($this->testPropName, $filtered->getEmptyDestinationProperties()); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Test transitive property existence. |
| 151 | + */ |
| 152 | + public function testTransitiveExistence() : void { |
| 153 | + $row = (new Row( |
| 154 | + [ |
| 155 | + 'a' => 'a', |
| 156 | + 'b' => 'b', |
| 157 | + ], |
| 158 | + ['a' => 'a'], |
| 159 | + )) |
| 160 | + ->freezeSource(); |
| 161 | + |
| 162 | + $transitive_prop = $this->randomMachineName(); |
| 163 | + |
| 164 | + $this->getInstance(['source' => 'b']) |
| 165 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 166 | + $row->setDestinationProperty($this->testPropName, 'b'); |
| 167 | + $this->getInstance(['source' => "@{$this->testPropName}"]) |
| 168 | + ->transform(NULL, $this->mockExecutable, $row, $transitive_prop); |
| 169 | + $row->setDestinationProperty($transitive_prop, 'b'); |
| 170 | + $this->assertTrue($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$transitive_prop]); |
| 171 | + |
| 172 | + $filtered = TrackingGet::filterRow($row); |
| 173 | + $this->assertNotContains($transitive_prop, $filtered->getEmptyDestinationProperties()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Test transitive property absence. |
| 178 | + */ |
| 179 | + public function testTransitiveAbsence() : void { |
| 180 | + $row = (new Row( |
| 181 | + ['a' => 'a'], |
| 182 | + ['a' => 'a'], |
| 183 | + )) |
| 184 | + ->freezeSource(); |
| 185 | + |
| 186 | + $transitive_prop = $this->randomMachineName(); |
| 187 | + |
| 188 | + $this->getInstance(['source' => 'b']) |
| 189 | + ->transform(NULL, $this->mockExecutable, $row, $this->testPropName); |
| 190 | + // Approximate behavior MigrateExecutable. |
| 191 | + // @see https://git.drupalcode.org/project/drupal/-/blob/10.5.x/core/modules/migrate/src/MigrateExecutable.php?ref_type=heads#L476 |
| 192 | + $row->setEmptyDestinationProperty($this->testPropName); |
| 193 | + $this->getInstance(['source' => "@{$this->testPropName}"]) |
| 194 | + ->transform(NULL, $this->mockExecutable, $row, $transitive_prop); |
| 195 | + $row->setEmptyDestinationProperty($transitive_prop); |
| 196 | + $this->assertFalse($row->getDestinationProperty(TrackingGet::PROPERTY_NAME)[$transitive_prop]); |
| 197 | + |
| 198 | + $filtered = TrackingGet::filterRow($row); |
| 199 | + $this->assertNull($filtered->getDestinationProperty($transitive_prop)); |
| 200 | + $this->assertNotContains($transitive_prop, $filtered->getEmptyDestinationProperties()); |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments