1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
9
|
|
|
|
10
|
|
|
class Issue3951Test extends AbstractFunctional |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider providerType |
14
|
|
|
*/ |
15
|
|
|
public function testIssue2266(string $type): void |
16
|
|
|
{ |
17
|
|
|
$spreadsheet = new Spreadsheet(); |
18
|
|
|
|
19
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
20
|
|
|
$sheet->setTitle('sorttrue'); |
21
|
|
|
$sheet->getCell('A1')->setValue(10); |
22
|
|
|
$sheet->getCell('A2')->setValue(5); |
23
|
|
|
$sheet->getCell('B1')->setValue(15); |
24
|
|
|
$protection = $sheet->getProtection(); |
25
|
|
|
$protection->setPassword('testpassword'); |
26
|
|
|
$protection->setSheet(true); |
27
|
|
|
$protection->setInsertRows(true); |
28
|
|
|
$protection->setFormatCells(true); |
29
|
|
|
$protection->setObjects(true); |
30
|
|
|
$protection->setAutoFilter(false); |
31
|
|
|
$protection->setSort(true); |
32
|
|
|
|
33
|
|
|
$sheet = $spreadsheet->createSheet(); |
34
|
|
|
$sheet->setTitle('sortfalse'); |
35
|
|
|
$sheet->getCell('A1')->setValue(10); |
36
|
|
|
$sheet->getCell('A2')->setValue(5); |
37
|
|
|
$sheet->getCell('B1')->setValue(15); |
38
|
|
|
$protection = $sheet->getProtection(); |
39
|
|
|
$protection->setPassword('testpassword'); |
40
|
|
|
$protection->setSheet(true); |
41
|
|
|
$protection->setInsertRows(true); |
42
|
|
|
$protection->setFormatCells(true); |
43
|
|
|
$protection->setObjects(true); |
44
|
|
|
$protection->setAutoFilter(false); |
45
|
|
|
$protection->setSort(false); |
46
|
|
|
|
47
|
|
|
$sheet = $spreadsheet->createSheet(); |
48
|
|
|
$sheet->setTitle('sortfalsenocolpw'); |
49
|
|
|
$sheet->getCell('A1')->setValue(10); |
50
|
|
|
$sheet->getCell('A2')->setValue(5); |
51
|
|
|
$sheet->getCell('C1')->setValue(15); |
52
|
|
|
$protection = $sheet->getProtection(); |
53
|
|
|
$protection->setPassword('testpassword'); |
54
|
|
|
$protection->setSheet(true); |
55
|
|
|
$protection->setInsertRows(true); |
56
|
|
|
$protection->setFormatCells(true); |
57
|
|
|
$protection->setObjects(true); |
58
|
|
|
$protection->setAutoFilter(false); |
59
|
|
|
$protection->setSort(false); |
60
|
|
|
$sheet->protectCells('A:A'); |
61
|
|
|
|
62
|
|
|
$sheet = $spreadsheet->createSheet(); |
63
|
|
|
$sheet->setTitle('sortfalsecolpw'); |
64
|
|
|
$sheet->getCell('A1')->setValue(10); |
65
|
|
|
$sheet->getCell('A2')->setValue(5); |
66
|
|
|
$sheet->getCell('C1')->setValue(15); |
67
|
|
|
$protection = $sheet->getProtection(); |
68
|
|
|
$protection->setPassword('testpassword'); |
69
|
|
|
$protection->setSheet(true); |
70
|
|
|
$protection->setInsertRows(true); |
71
|
|
|
$protection->setFormatCells(true); |
72
|
|
|
$protection->setObjects(true); |
73
|
|
|
$protection->setAutoFilter(false); |
74
|
|
|
$protection->setSort(false); |
75
|
|
|
$sheet->protectCells('A:A', 'sortpw', false, 'sortrange'); |
76
|
|
|
|
77
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type); |
78
|
|
|
$spreadsheet->disconnectWorksheets(); |
79
|
|
|
|
80
|
|
|
self::assertCount(4, $reloadedSpreadsheet->getAllSheets()); |
81
|
|
|
|
82
|
|
|
$rsheet1 = $reloadedSpreadsheet->getSheetByNameOrThrow('sorttrue'); |
83
|
|
|
$protection = $rsheet1->getProtection(); |
84
|
|
|
self::assertNotEquals('', $protection->getPassword()); |
85
|
|
|
self::assertTrue($protection->getSort()); |
86
|
|
|
self::assertEmpty($rsheet1->getProtectedCellRanges()); |
87
|
|
|
|
88
|
|
|
$rsheet2 = $reloadedSpreadsheet->getSheetByNameOrThrow('sortfalse'); |
89
|
|
|
$protection = $rsheet2->getProtection(); |
90
|
|
|
self::assertNotEquals('', $protection->getPassword()); |
91
|
|
|
self::assertFalse($protection->getSort()); |
92
|
|
|
self::assertEmpty($rsheet2->getProtectedCellRanges()); |
93
|
|
|
|
94
|
|
|
$rsheet3 = $reloadedSpreadsheet->getSheetByNameOrThrow('sortfalsenocolpw'); |
95
|
|
|
$protection = $rsheet3->getProtection(); |
96
|
|
|
self::assertNotEquals('', $protection->getPassword()); |
97
|
|
|
self::assertFalse($protection->getSort()); |
98
|
|
|
$maxRow = ($type === 'Xlsx') ? 1048576 : 65536; |
99
|
|
|
$testKey = "A1:A$maxRow"; |
100
|
|
|
$protectedCells = $rsheet3->getProtectedCellRanges(); |
101
|
|
|
self::assertCount(1, $protectedCells); |
102
|
|
|
self::assertArrayHasKey($testKey, $protectedCells); |
103
|
|
|
self::assertSame('', $protectedCells[$testKey]->getPassword()); |
104
|
|
|
|
105
|
|
|
$rsheet4 = $reloadedSpreadsheet->getSheetByNameOrThrow('sortfalsecolpw'); |
106
|
|
|
$protection = $rsheet4->getProtection(); |
107
|
|
|
self::assertNotEquals('', $protection->getPassword()); |
108
|
|
|
self::assertFalse($protection->getSort()); |
109
|
|
|
$maxRow = ($type === 'Xlsx') ? 1048576 : 65536; |
110
|
|
|
$testKey = "A1:A$maxRow"; |
111
|
|
|
$protectedCells = $rsheet4->getProtectedCellRanges(); |
112
|
|
|
self::assertCount(1, $protectedCells); |
113
|
|
|
self::assertArrayHasKey($testKey, $protectedCells); |
114
|
|
|
self::assertNotEquals('', $protectedCells[$testKey]->getPassword()); |
115
|
|
|
if ($type === 'Xlsx') { |
116
|
|
|
self::assertSame('sortrange', $protectedCells[$testKey]->getName()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$reloadedSpreadsheet->disconnectWorksheets(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function providerType(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
['Xlsx'], |
126
|
|
|
['Xls'], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|