|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Cell\DataValidation; |
| 8 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 9 | +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class ReferenceHelperDVTest extends TestCase |
| 13 | +{ |
| 14 | + |
| 15 | + public function testInsertRowsWithDataValidation(): void |
| 16 | + { |
| 17 | + $spreadsheet = new Spreadsheet(); |
| 18 | + $sheet = $spreadsheet->getActiveSheet(); |
| 19 | + |
| 20 | + $sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true); |
| 21 | + $cellAddress = 'E5'; |
| 22 | + $this->setDataValidation($sheet, $cellAddress); |
| 23 | + |
| 24 | + $sheet->insertNewRowBefore(2, 2); |
| 25 | + |
| 26 | + self::assertFalse( |
| 27 | + $sheet->getCell($cellAddress)->hasDataValidation() |
| 28 | + ); |
| 29 | + self::assertTrue($sheet->getCell('E7')->hasDataValidation()); |
| 30 | + self::assertSame('E7', $sheet->getDataValidation('E7')->getSqref()); |
| 31 | + self::assertSame('$A$7:$A$10', $sheet->getDataValidation('E7')->getFormula1()); |
| 32 | + $spreadsheet->disconnectWorksheets(); |
| 33 | + } |
| 34 | + |
| 35 | + public function testDeleteRowsWithDataValidation(): void |
| 36 | + { |
| 37 | + $spreadsheet = new Spreadsheet(); |
| 38 | + $sheet = $spreadsheet->getActiveSheet(); |
| 39 | + |
| 40 | + $sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true); |
| 41 | + $cellAddress = 'E5'; |
| 42 | + $this->setDataValidation($sheet, $cellAddress); |
| 43 | + |
| 44 | + $sheet->removeRow(2, 2); |
| 45 | + |
| 46 | + self::assertFalse( |
| 47 | + $sheet->getCell($cellAddress)->hasDataValidation() |
| 48 | + ); |
| 49 | + self::assertTrue($sheet->getCell('E3')->hasDataValidation()); |
| 50 | + self::assertSame('E3', $sheet->getDataValidation('E3')->getSqref()); |
| 51 | + self::assertSame('$A$3:$A$6', $sheet->getDataValidation('E3')->getFormula1()); |
| 52 | + |
| 53 | + $spreadsheet->disconnectWorksheets(); |
| 54 | + } |
| 55 | + |
| 56 | + public function testDeleteColumnsWithDataValidation(): void |
| 57 | + { |
| 58 | + $spreadsheet = new Spreadsheet(); |
| 59 | + $sheet = $spreadsheet->getActiveSheet(); |
| 60 | + |
| 61 | + $sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true); |
| 62 | + $cellAddress = 'E5'; |
| 63 | + $this->setDataValidation($sheet, $cellAddress); |
| 64 | + |
| 65 | + $sheet->removeColumn('B', 2); |
| 66 | + |
| 67 | + self::assertFalse( |
| 68 | + $sheet->getCell($cellAddress)->hasDataValidation() |
| 69 | + ); |
| 70 | + self::assertTrue($sheet->getCell('C5')->hasDataValidation()); |
| 71 | + self::assertSame('C5', $sheet->getDataValidation('C5')->getSqref()); |
| 72 | + self::assertSame('$A$5:$A$8', $sheet->getDataValidation('C5')->getFormula1()); |
| 73 | + $spreadsheet->disconnectWorksheets(); |
| 74 | + } |
| 75 | + |
| 76 | + public function testInsertColumnsWithDataValidation(): void |
| 77 | + { |
| 78 | + $spreadsheet = new Spreadsheet(); |
| 79 | + $sheet = $spreadsheet->getActiveSheet(); |
| 80 | + |
| 81 | + $sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true); |
| 82 | + $cellAddress = 'E5'; |
| 83 | + $this->setDataValidation($sheet, $cellAddress); |
| 84 | + |
| 85 | + $sheet->insertNewColumnBefore('C', 2); |
| 86 | + |
| 87 | + self::assertFalse( |
| 88 | + $sheet->getCell($cellAddress)->hasDataValidation() |
| 89 | + ); |
| 90 | + self::assertTrue($sheet->getCell('G5')->hasDataValidation()); |
| 91 | + self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref()); |
| 92 | + self::assertSame('$A$5:$A$8', $sheet->getDataValidation('G5')->getFormula1()); |
| 93 | + $spreadsheet->disconnectWorksheets(); |
| 94 | + } |
| 95 | + |
| 96 | + public function testInsertColumnsWithDataValidation2(): void |
| 97 | + { |
| 98 | + $spreadsheet = new Spreadsheet(); |
| 99 | + $sheet = $spreadsheet->getActiveSheet(); |
| 100 | + |
| 101 | + $sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true); |
| 102 | + $cellAddress = 'E5'; |
| 103 | + $this->setDataValidation($sheet, $cellAddress); |
| 104 | + |
| 105 | + $sheet->insertNewColumnBefore('A', 2); |
| 106 | + |
| 107 | + self::assertFalse( |
| 108 | + $sheet->getCell($cellAddress)->hasDataValidation() |
| 109 | + ); |
| 110 | + self::assertTrue($sheet->getCell('G5')->hasDataValidation()); |
| 111 | + self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref()); |
| 112 | + self::assertSame('$C$5:$C$8', $sheet->getDataValidation('G5')->getFormula1()); |
| 113 | + $spreadsheet->disconnectWorksheets(); |
| 114 | + } |
| 115 | + |
| 116 | + private function setDataValidation(Worksheet $sheet, string $cellAddress): void |
| 117 | + { |
| 118 | + $validation = $sheet->getCell($cellAddress) |
| 119 | + ->getDataValidation(); |
| 120 | + $validation->setType(DataValidation::TYPE_LIST); |
| 121 | + $validation->setErrorStyle( |
| 122 | + DataValidation::STYLE_STOP |
| 123 | + ); |
| 124 | + $validation->setAllowBlank(false); |
| 125 | + $validation->setShowInputMessage(true); |
| 126 | + $validation->setShowErrorMessage(true); |
| 127 | + $validation->setShowDropDown(true); |
| 128 | + $validation->setErrorTitle('Input error'); |
| 129 | + $validation->setError('Value is not in list.'); |
| 130 | + $validation->setPromptTitle('Pick from list'); |
| 131 | + $validation->setPrompt('Please pick a value from the drop-down list.'); |
| 132 | + $validation->setFormula1('$A$5:$A$8'); |
| 133 | + } |
| 134 | +} |
0 commit comments