Skip to content

Commit 576fbc4

Browse files
author
Mark Baker
authored
Merge pull request #2690 from PHPOffice/UnitTests_ReferenceHelper-DataValidation
More unit testing for inserting/deleting rows/columns with DataValida…
2 parents 178f748 + f6fcc4d commit 576fbc4

File tree

1 file changed

+211
-12
lines changed

1 file changed

+211
-12
lines changed

tests/PhpSpreadsheetTests/ReferenceHelperTest.php

Lines changed: 211 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,83 @@ function (Hyperlink $value) {
298298
self::assertSame(['A3' => 'https://phpspreadsheet.readthedocs.io/en/latest/'], $hyperlinks);
299299
}
300300

301+
public function testInsertRowsWithDataValidation(): void
302+
{
303+
$spreadsheet = new Spreadsheet();
304+
$sheet = $spreadsheet->getActiveSheet();
305+
306+
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
307+
$cellAddress = 'E5';
308+
$this->setDataValidation($sheet, $cellAddress);
309+
310+
$sheet->insertNewRowBefore(2, 2);
311+
312+
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
313+
self::assertTrue($sheet->getCell('E7')->hasDataValidation());
314+
}
315+
316+
public function testDeleteRowsWithDataValidation(): void
317+
{
318+
$spreadsheet = new Spreadsheet();
319+
$sheet = $spreadsheet->getActiveSheet();
320+
321+
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
322+
$cellAddress = 'E5';
323+
$this->setDataValidation($sheet, $cellAddress);
324+
325+
$sheet->removeRow(2, 2);
326+
327+
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
328+
self::assertTrue($sheet->getCell('E3')->hasDataValidation());
329+
}
330+
331+
public function testDeleteColumnsWithDataValidation(): void
332+
{
333+
$spreadsheet = new Spreadsheet();
334+
$sheet = $spreadsheet->getActiveSheet();
335+
336+
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
337+
$cellAddress = 'E5';
338+
$this->setDataValidation($sheet, $cellAddress);
339+
340+
$sheet->removeColumn('B', 2);
341+
342+
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
343+
self::assertTrue($sheet->getCell('C5')->hasDataValidation());
344+
}
345+
346+
public function testInsertColumnsWithDataValidation(): void
347+
{
348+
$spreadsheet = new Spreadsheet();
349+
$sheet = $spreadsheet->getActiveSheet();
350+
351+
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
352+
$cellAddress = 'E5';
353+
$this->setDataValidation($sheet, $cellAddress);
354+
355+
$sheet->insertNewColumnBefore('C', 2);
356+
357+
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
358+
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
359+
}
360+
361+
private function setDataValidation(Worksheet $sheet, string $cellAddress): void
362+
{
363+
$validation = $sheet->getCell($cellAddress)
364+
->getDataValidation();
365+
$validation->setType(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
366+
$validation->setErrorStyle(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION);
367+
$validation->setAllowBlank(false);
368+
$validation->setShowInputMessage(true);
369+
$validation->setShowErrorMessage(true);
370+
$validation->setShowDropDown(true);
371+
$validation->setErrorTitle('Input error');
372+
$validation->setError('Value is not in list.');
373+
$validation->setPromptTitle('Pick from list');
374+
$validation->setPrompt('Please pick a value from the drop-down list.');
375+
$validation->setFormula1('$A5:$A8');
376+
}
377+
301378
public function testInsertRowsWithConditionalFormatting(): void
302379
{
303380
$spreadsheet = new Spreadsheet();
@@ -306,6 +383,92 @@ public function testInsertRowsWithConditionalFormatting(): void
306383
$sheet->getCell('H5')->setValue(5);
307384

308385
$cellRange = 'C3:F7';
386+
$this->setConditionalFormatting($sheet, $cellRange);
387+
388+
$sheet->insertNewRowBefore(4, 2);
389+
390+
$styles = $sheet->getConditionalStylesCollection();
391+
// verify that the conditional range has been updated
392+
self::assertSame('C3:F9', array_keys($styles)[0]);
393+
// verify that the conditions have been updated
394+
foreach ($styles as $style) {
395+
foreach ($style as $conditions) {
396+
self::assertSame('$H$7', $conditions->getConditions()[0]);
397+
}
398+
}
399+
}
400+
401+
public function testInsertColumnssWithConditionalFormatting(): void
402+
{
403+
$spreadsheet = new Spreadsheet();
404+
$sheet = $spreadsheet->getActiveSheet();
405+
$sheet->fromArray([[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10], [9, 10, 11, 12]], null, 'C3', true);
406+
$sheet->getCell('H5')->setValue(5);
407+
408+
$cellRange = 'C3:F7';
409+
$this->setConditionalFormatting($sheet, $cellRange);
410+
411+
$sheet->insertNewColumnBefore('C', 2);
412+
413+
$styles = $sheet->getConditionalStylesCollection();
414+
// verify that the conditional range has been updated
415+
self::assertSame('E3:H7', array_keys($styles)[0]);
416+
// verify that the conditions have been updated
417+
foreach ($styles as $style) {
418+
foreach ($style as $conditions) {
419+
self::assertSame('$J$5', $conditions->getConditions()[0]);
420+
}
421+
}
422+
}
423+
424+
public function testDeleteRowsWithConditionalFormatting(): void
425+
{
426+
$spreadsheet = new Spreadsheet();
427+
$sheet = $spreadsheet->getActiveSheet();
428+
$sheet->fromArray([[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10], [9, 10, 11, 12]], null, 'C3', true);
429+
$sheet->getCell('H5')->setValue(5);
430+
431+
$cellRange = 'C3:F7';
432+
$this->setConditionalFormatting($sheet, $cellRange);
433+
434+
$sheet->removeRow(4, 2);
435+
436+
$styles = $sheet->getConditionalStylesCollection();
437+
// verify that the conditional range has been updated
438+
self::assertSame('C3:F5', array_keys($styles)[0]);
439+
// verify that the conditions have been updated
440+
foreach ($styles as $style) {
441+
foreach ($style as $conditions) {
442+
self::assertSame('$H$5', $conditions->getConditions()[0]);
443+
}
444+
}
445+
}
446+
447+
public function testDeleteColumnsWithConditionalFormatting(): void
448+
{
449+
$spreadsheet = new Spreadsheet();
450+
$sheet = $spreadsheet->getActiveSheet();
451+
$sheet->fromArray([[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10], [9, 10, 11, 12]], null, 'C3', true);
452+
$sheet->getCell('H5')->setValue(5);
453+
454+
$cellRange = 'C3:F7';
455+
$this->setConditionalFormatting($sheet, $cellRange);
456+
457+
$sheet->removeColumn('D', 2);
458+
459+
$styles = $sheet->getConditionalStylesCollection();
460+
// verify that the conditional range has been updated
461+
self::assertSame('C3:D7', array_keys($styles)[0]);
462+
// verify that the conditions have been updated
463+
foreach ($styles as $style) {
464+
foreach ($style as $conditions) {
465+
self::assertSame('$F$5', $conditions->getConditions()[0]);
466+
}
467+
}
468+
}
469+
470+
private function setConditionalFormatting(Worksheet $sheet, string $cellRange): void
471+
{
309472
$conditionalStyles = [];
310473
$wizardFactory = new Wizard($cellRange);
311474
/** @var Wizard\CellValue $cellWizard */
@@ -320,19 +483,55 @@ public function testInsertRowsWithConditionalFormatting(): void
320483
$cellWizard->lessThan('$H$5', Wizard::VALUE_TYPE_CELL);
321484
$conditionalStyles[] = $cellWizard->getConditional();
322485

323-
$spreadsheet->getActiveSheet()
324-
->getStyle($cellWizard->getCellRange())
486+
$sheet->getStyle($cellWizard->getCellRange())
325487
->setConditionalStyles($conditionalStyles);
326-
$sheet->insertNewRowBefore(4, 2);
488+
}
327489

328-
$styles = $sheet->getConditionalStylesCollection();
329-
// verify that the conditional range has been updated
330-
self::assertSame('C3:F9', array_keys($styles)[0]);
331-
// verify that the conditions have been updated
332-
foreach ($styles as $style) {
333-
foreach ($style as $conditions) {
334-
self::assertSame('$H$7', $conditions->getConditions()[0]);
335-
}
336-
}
490+
public function testInsertRowsWithPrintArea(): void
491+
{
492+
$spreadsheet = new Spreadsheet();
493+
$sheet = $spreadsheet->getActiveSheet();
494+
$sheet->getPageSetup()->setPrintArea('A1:J10');
495+
496+
$sheet->insertNewRowBefore(2, 2);
497+
498+
$printArea = $sheet->getPageSetup()->getPrintArea();
499+
self::assertSame('A1:J12', $printArea);
500+
}
501+
502+
public function testInsertColumnsWithPrintArea(): void
503+
{
504+
$spreadsheet = new Spreadsheet();
505+
$sheet = $spreadsheet->getActiveSheet();
506+
$sheet->getPageSetup()->setPrintArea('A1:J10');
507+
508+
$sheet->insertNewColumnBefore('B', 2);
509+
510+
$printArea = $sheet->getPageSetup()->getPrintArea();
511+
self::assertSame('A1:L10', $printArea);
512+
}
513+
514+
public function testDeleteRowsWithPrintArea(): void
515+
{
516+
$spreadsheet = new Spreadsheet();
517+
$sheet = $spreadsheet->getActiveSheet();
518+
$sheet->getPageSetup()->setPrintArea('A1:J10');
519+
520+
$sheet->removeRow(2, 2);
521+
522+
$printArea = $sheet->getPageSetup()->getPrintArea();
523+
self::assertSame('A1:J8', $printArea);
524+
}
525+
526+
public function testDeleteColumnsWithPrintArea(): void
527+
{
528+
$spreadsheet = new Spreadsheet();
529+
$sheet = $spreadsheet->getActiveSheet();
530+
$sheet->getPageSetup()->setPrintArea('A1:J10');
531+
532+
$sheet->removeColumn('B', 2);
533+
534+
$printArea = $sheet->getPageSetup()->getPrintArea();
535+
self::assertSame('A1:H10', $printArea);
337536
}
338537
}

0 commit comments

Comments
 (0)