Skip to content

Commit 2c73701

Browse files
authored
Merge pull request #216 from asgrim/fix-ini-symlinks-being-replaced-on-uninstall
Fix INI symlinks being replaced with real files on uninstall
2 parents 630c398 + 21ccfeb commit 2c73701

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/Installing/Ini/RemoveIniEntryWithFileGetContents.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ static function (string $path) use ($additionalIniDirectory): bool {
5959
);
6060
}
6161

62+
// Make sure all symlinks are resolved
63+
$allIniFiles = array_filter(array_map('realpath', $allIniFiles));
64+
6265
$regex = sprintf(
6366
'/^(%s\s*=\s*%s)/m',
6467
$package->extensionType() === ExtensionType::PhpModule ? 'extension' : 'zend_extension',

test/unit/Installing/Ini/RemoveIniEntryWithFileGetContentsTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@
1818
use Php\Pie\Platform\ThreadSafetyMode;
1919
use PHPUnit\Framework\Attributes\CoversClass;
2020
use PHPUnit\Framework\Attributes\DataProvider;
21+
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
2122
use PHPUnit\Framework\TestCase;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Webmozart\Assert\Assert;
2425

2526
use function file_get_contents;
2627
use function file_put_contents;
28+
use function is_link;
2729
use function mkdir;
30+
use function realpath;
31+
use function symlink;
2832
use function sys_get_temp_dir;
33+
use function tempnam;
2934
use function uniqid;
35+
use function unlink;
3036

3137
use const DIRECTORY_SEPARATOR;
3238

@@ -42,7 +48,7 @@ public function setUp(): void
4248
{
4349
parent::setUp();
4450

45-
$this->iniFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_remove_ini_test', true);
51+
$this->iniFilePath = realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . uniqid('pie_remove_ini_test', true);
4652
mkdir($this->iniFilePath);
4753
Assert::positiveInteger(file_put_contents(
4854
$this->iniFilePath . DIRECTORY_SEPARATOR . 'with_commented_exts.ini',
@@ -121,4 +127,66 @@ public function testRelevantIniFilesHaveExtensionRemoved(ExtensionType $extensio
121127
file_get_contents($this->iniFilePath . DIRECTORY_SEPARATOR . 'with_active_exts.ini'),
122128
);
123129
}
130+
131+
#[RequiresOperatingSystemFamily('Linux')]
132+
public function testSymlinkedIniFilesAreResolved(): void
133+
{
134+
$realIni = $this->iniFilePath . DIRECTORY_SEPARATOR . 'with_active_exts.ini';
135+
$symlinkedIni = tempnam(sys_get_temp_dir(), 'pie_ini_removal_test_link_');
136+
unlink($symlinkedIni);
137+
symlink($realIni, $symlinkedIni);
138+
self::assertTrue(is_link($symlinkedIni));
139+
140+
$phpBinaryPath = $this->createMock(PhpBinaryPath::class);
141+
$phpBinaryPath
142+
->method('loadedIniConfigurationFile')
143+
->willReturn($symlinkedIni);
144+
$phpBinaryPath
145+
->method('additionalIniDirectory')
146+
->willReturn(null);
147+
148+
$package = new Package(
149+
$this->createMock(CompletePackageInterface::class),
150+
ExtensionType::PhpModule,
151+
ExtensionName::normaliseFromString('foobar'),
152+
'foobar/foobar',
153+
'1.2.3',
154+
null,
155+
);
156+
157+
$targetPlatform = new TargetPlatform(
158+
OperatingSystem::NonWindows,
159+
OperatingSystemFamily::Linux,
160+
$phpBinaryPath,
161+
Architecture::x86_64,
162+
ThreadSafetyMode::ThreadSafe,
163+
1,
164+
null,
165+
);
166+
167+
$affectedFiles = (new RemoveIniEntryWithFileGetContents())(
168+
$package,
169+
$targetPlatform,
170+
$this->createMock(OutputInterface::class),
171+
);
172+
173+
self::assertSame(
174+
[$realIni],
175+
$affectedFiles,
176+
);
177+
178+
self::assertTrue(is_link($symlinkedIni));
179+
180+
$expectedIniContent = "; extension=foobar ; removed by PIE\nzend_extension=foobar\n";
181+
self::assertSame(
182+
$expectedIniContent,
183+
file_get_contents($realIni),
184+
);
185+
self::assertSame(
186+
$expectedIniContent,
187+
file_get_contents($symlinkedIni),
188+
);
189+
190+
unlink($symlinkedIni);
191+
}
124192
}

0 commit comments

Comments
 (0)