 50a9bc83ab
			
		
	
	
		50a9bc83ab
		
	
	
	
	
		
			
			When extracting sheet title from string reference (like `"Work!sheet1!A1"`), PHP function `explode()` divide this string into three parts: `['Work', 'sheet1', 'A1']`. And then these wrong values are used in formulas, ranges, etc. This change fix that problem by using special function `Worksheet::extractSheetTitle()`. This function also has been changed to make sure that worksheet title can contain "!" character. So, that function search last position of "!" in reference string and divide it to 2 parts correctly: `['Work!sheet1', 'A1']`. Fixes #325 Fixes #662
		
			
				
	
	
		
			160 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
 | |
| 
 | |
| use PhpOffice\PhpSpreadsheet\Spreadsheet;
 | |
| use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
 | |
| use PHPUnit\Framework\TestCase;
 | |
| 
 | |
| class WorksheetTest extends TestCase
 | |
| {
 | |
|     public function testSetTitle()
 | |
|     {
 | |
|         $testTitle = str_repeat('a', 31);
 | |
| 
 | |
|         $worksheet = new Worksheet();
 | |
|         $worksheet->setTitle($testTitle);
 | |
|         self::assertSame($testTitle, $worksheet->getTitle());
 | |
|     }
 | |
| 
 | |
|     public function setTitleInvalidProvider()
 | |
|     {
 | |
|         return [
 | |
|             [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet title.'],
 | |
|             ['invalid*title', 'Invalid character found in sheet title'],
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param string $title
 | |
|      * @param string $expectMessage
 | |
|      * @dataProvider setTitleInvalidProvider
 | |
|      */
 | |
|     public function testSetTitleInvalid($title, $expectMessage)
 | |
|     {
 | |
|         // First, test setting title with validation disabled -- should be successful
 | |
|         $worksheet = new Worksheet();
 | |
|         $worksheet->setTitle($title, true, false);
 | |
| 
 | |
|         // Next, test again with validation enabled -- this time we should fail
 | |
|         $worksheet = new Worksheet();
 | |
|         $this->expectException(\Exception::class);
 | |
|         $this->expectExceptionMessage($expectMessage);
 | |
|         $worksheet->setTitle($title);
 | |
|     }
 | |
| 
 | |
|     public function testSetTitleDuplicate()
 | |
|     {
 | |
|         // Create a Spreadsheet with three Worksheets (the first is created automatically)
 | |
|         $spreadsheet = new Spreadsheet();
 | |
|         $spreadsheet->createSheet();
 | |
|         $spreadsheet->createSheet();
 | |
| 
 | |
|         // Set unique title -- should be unchanged
 | |
|         $sheet = $spreadsheet->getSheet(0);
 | |
|         $sheet->setTitle('Test Title');
 | |
|         self::assertSame('Test Title', $sheet->getTitle());
 | |
| 
 | |
|         // Set duplicate title -- should have numeric suffix appended
 | |
|         $sheet = $spreadsheet->getSheet(1);
 | |
|         $sheet->setTitle('Test Title');
 | |
|         self::assertSame('Test Title 1', $sheet->getTitle());
 | |
| 
 | |
|         // Set duplicate title with validation disabled -- should be unchanged
 | |
|         $sheet = $spreadsheet->getSheet(2);
 | |
|         $sheet->setTitle('Test Title', true, false);
 | |
|         self::assertSame('Test Title', $sheet->getTitle());
 | |
|     }
 | |
| 
 | |
|     public function testSetCodeName()
 | |
|     {
 | |
|         $testCodeName = str_repeat('a', 31);
 | |
| 
 | |
|         $worksheet = new Worksheet();
 | |
|         $worksheet->setCodeName($testCodeName);
 | |
|         self::assertSame($testCodeName, $worksheet->getCodeName());
 | |
|     }
 | |
| 
 | |
|     public function setCodeNameInvalidProvider()
 | |
|     {
 | |
|         return [
 | |
|             [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet code name.'],
 | |
|             ['invalid*code*name', 'Invalid character found in sheet code name'],
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param string $codeName
 | |
|      * @param string $expectMessage
 | |
|      * @dataProvider setCodeNameInvalidProvider
 | |
|      */
 | |
|     public function testSetCodeNameInvalid($codeName, $expectMessage)
 | |
|     {
 | |
|         // First, test setting code name with validation disabled -- should be successful
 | |
|         $worksheet = new Worksheet();
 | |
|         $worksheet->setCodeName($codeName, false);
 | |
| 
 | |
|         // Next, test again with validation enabled -- this time we should fail
 | |
|         $worksheet = new Worksheet();
 | |
|         $this->expectException(\Exception::class);
 | |
|         $this->expectExceptionMessage($expectMessage);
 | |
|         $worksheet->setCodeName($codeName);
 | |
|     }
 | |
| 
 | |
|     public function testSetCodeNameDuplicate()
 | |
|     {
 | |
|         // Create a Spreadsheet with three Worksheets (the first is created automatically)
 | |
|         $spreadsheet = new Spreadsheet();
 | |
|         $spreadsheet->createSheet();
 | |
|         $spreadsheet->createSheet();
 | |
| 
 | |
|         // Set unique code name -- should be massaged to Snake_Case
 | |
|         $sheet = $spreadsheet->getSheet(0);
 | |
|         $sheet->setCodeName('Test Code Name');
 | |
|         self::assertSame('Test_Code_Name', $sheet->getCodeName());
 | |
| 
 | |
|         // Set duplicate code name -- should be massaged and have numeric suffix appended
 | |
|         $sheet = $spreadsheet->getSheet(1);
 | |
|         $sheet->setCodeName('Test Code Name');
 | |
|         self::assertSame('Test_Code_Name_1', $sheet->getCodeName());
 | |
| 
 | |
|         // Set duplicate code name with validation disabled -- should be unchanged, and unmassaged
 | |
|         $sheet = $spreadsheet->getSheet(2);
 | |
|         $sheet->setCodeName('Test Code Name', false);
 | |
|         self::assertSame('Test Code Name', $sheet->getCodeName());
 | |
|     }
 | |
| 
 | |
|     public function testFreezePaneSelectedCell()
 | |
|     {
 | |
|         $worksheet = new Worksheet();
 | |
|         $worksheet->freezePane('B2');
 | |
|         self::assertSame('B2', $worksheet->getTopLeftCell());
 | |
|     }
 | |
| 
 | |
|     public function extractSheetTitleProvider()
 | |
|     {
 | |
|         return [
 | |
|             ['B2', '', '', 'B2'],
 | |
|             ['testTitle!B2', 'testTitle', 'B2', 'B2'],
 | |
|             ['test!Title!B2', 'test!Title', 'B2', 'B2'],
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param string $range
 | |
|      * @param string $expectTitle
 | |
|      * @param string $expectCell
 | |
|      * @param string $expectCell2
 | |
|      * @dataProvider extractSheetTitleProvider
 | |
|      */
 | |
|     public function testExtractSheetTitle($range, $expectTitle, $expectCell, $expectCell2)
 | |
|     {
 | |
|         // only cell reference
 | |
|         self::assertSame($expectCell, Worksheet::extractSheetTitle($range));
 | |
|         // with title in array
 | |
|         $arRange = Worksheet::extractSheetTitle($range, true);
 | |
|         self::assertSame($expectTitle, $arRange[0]);
 | |
|         self::assertSame($expectCell2, $arRange[1]);
 | |
|     }
 | |
| }
 |