Resources
5 years ago
AzureTest.php
1 year ago
ContactTest.php
5 years ago
DeltaQueryTest.php
1 year ago
EventTest.php
5 years ago
ExcelTest.php
1 year ago
GraphTestBase.php
1 year ago
MailTest.php
1 year ago
OnedriveTest.php
1 year ago
OnenoteTest.php
1 year ago
OpenTypeTest.php
1 year ago
PlannerTest.php
1 year ago
SharepointTest.php
1 year ago
TermStoreTest.php
1 year ago
TestConstants.php
1 year ago
UserTest.php
1 year ago
WebhooksTest.php
1 year ago
ExcelTest.php
322 lines
| 1 | <?php |
| 2 | use PHPUnit\Framework\TestCase; |
| 3 | use Microsoft\Graph\Test\GraphTestBase; |
| 4 | use Microsoft\Graph\Model; |
| 5 | use AmeliaGuzzleHttp\Exception\RequestException; |
| 6 | |
| 7 | class ExcelTest extends TestCase |
| 8 | { |
| 9 | private $_client; |
| 10 | private $_fileId; |
| 11 | |
| 12 | protected function setUp(): void |
| 13 | { |
| 14 | $graphTestBase = new GraphTestBase(); |
| 15 | $this->_client = $graphTestBase->graphClient; |
| 16 | |
| 17 | $this->_fileId = $this->createTestFile('_excelTestResource'.rand().'.xlsx'); |
| 18 | $this->uploadTestFileContent($this->_fileId); |
| 19 | } |
| 20 | |
| 21 | protected function tearDown(): void |
| 22 | { |
| 23 | $this->deleteTestFile($this->_fileId); |
| 24 | } |
| 25 | |
| 26 | private function createTestFile($filename) |
| 27 | { |
| 28 | $excelWorkbook = new Model\DriveItem(); |
| 29 | $excelWorkbook->setName($filename); |
| 30 | $file = new Model\File(); |
| 31 | $file->setODataType("microsoft.graph.file"); |
| 32 | $excelWorkbook->setFile($file); |
| 33 | |
| 34 | $excelWorkbookDriveItem = $this->_client->createRequest("POST", "/me/drive/root/children") |
| 35 | ->attachBody($excelWorkbook) |
| 36 | ->setReturnType(Model\DriveItem::class) |
| 37 | ->execute(); |
| 38 | $this->assertNotNull($excelWorkbookDriveItem); |
| 39 | |
| 40 | return $excelWorkbookDriveItem->getId(); |
| 41 | } |
| 42 | |
| 43 | private function deleteTestFile($fileId) |
| 44 | { |
| 45 | //After updating the doc, the service cannot immediately process the delete |
| 46 | sleep(4); |
| 47 | |
| 48 | $this->_client->createRequest("DELETE", "/me/drive/items/$fileId") |
| 49 | ->addHeaders(array("if-match" => "*")) |
| 50 | ->execute(); |
| 51 | } |
| 52 | |
| 53 | private function uploadTestFileContent($fileId) |
| 54 | { |
| 55 | $stream = AmeliaGuzzleHttp\Psr7\stream_for(fopen("./tests/Functional/Resources/excelTestResource.xlsx", "r")); |
| 56 | $excelDriveItem = $this->_client->createRequest("PUT", "/me/drive/items/" . $this->_fileId . "/content") |
| 57 | ->addHeaders(array( |
| 58 | "Content-Type" => "application/octet-stream", |
| 59 | "Content-Length" => filesize("./tests/Functional/Resources/excelTestResource.xlsx") |
| 60 | )) |
| 61 | ->attachBody($stream) |
| 62 | ->execute(); |
| 63 | $this->assertEquals(200, $excelDriveItem->getStatus()); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @group functional |
| 68 | * @group excel |
| 69 | */ |
| 70 | public function testGetUpdateRange() |
| 71 | { |
| 72 | $rangeToUpdate = $this->_client->createRequest( |
| 73 | "GET", |
| 74 | "/me/drive/items/" . |
| 75 | $this->_fileId . |
| 76 | "/workbook/worksheets/GetUpdateRange/Range(address='A1')" |
| 77 | ) |
| 78 | ->setReturnType(Model\WorkbookRange::class) |
| 79 | ->execute(); |
| 80 | $arr = $rangeToUpdate->getValues(); |
| 81 | |
| 82 | $arr[0][0] = "TestValueB"; |
| 83 | $dummyWorkbookRange = new Model\WorkbookRange(); |
| 84 | $dummyWorkbookRange->setValues($arr); |
| 85 | |
| 86 | $workbookRange = $this->_client->createRequest( |
| 87 | "PATCH", |
| 88 | "/me/drive/items/" . |
| 89 | $this->_fileId . |
| 90 | "/workbook/worksheets/GetUpdateRange/Range(address='A1')" |
| 91 | ) |
| 92 | ->attachBody($dummyWorkbookRange) |
| 93 | ->setReturnType(Model\WorkbookRange::class) |
| 94 | ->execute(); |
| 95 | $this->assertNotNull($workbookRange); |
| 96 | $this->assertEquals("TestValueB", $workbookRange->getValues()[0][0]); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @group functional |
| 101 | * @group excel |
| 102 | */ |
| 103 | public function testChangeNumberFormat() |
| 104 | { |
| 105 | $excelWorksheetName = "ChangeNumberFormat"; |
| 106 | $rangeAddress = "E2"; |
| 107 | |
| 108 | $arr = [["$#,##0.00;[Red]$#,##0.00"]]; |
| 109 | |
| 110 | $workbookRange = $this->_client->createRequest( |
| 111 | "PATCH", |
| 112 | "/me/drive/items/" . |
| 113 | $this->_fileId . |
| 114 | "/workbook/worksheets/$excelWorksheetName/range(address='$rangeAddress')" |
| 115 | ) |
| 116 | ->attachBody(array("numberFormat" => $arr)) |
| 117 | ->setReturnType(Model\WorkbookRange::class) |
| 118 | ->execute(); |
| 119 | $this->assertNotNull($workbookRange); |
| 120 | $this->assertEquals($arr, $workbookRange->getNumberFormat()); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @group functional |
| 125 | * @group excel |
| 126 | */ |
| 127 | public function testAbsFunc() |
| 128 | { |
| 129 | $inputNumber = "-10"; |
| 130 | |
| 131 | $workbookFunctionResult = $this->_client->createRequest( |
| 132 | "POST", |
| 133 | "/me/drive/items/" . |
| 134 | $this->_fileId . |
| 135 | "/workbook/functions/abs" |
| 136 | ) |
| 137 | ->attachBody('{"number": '. $inputNumber . '}') |
| 138 | ->setReturnType(Model\WorkbookFunctionResult::class) |
| 139 | ->execute(); |
| 140 | $this->assertNotNull($workbookFunctionResult); |
| 141 | $this->assertEquals("10", $workbookFunctionResult->getValue()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @group functional |
| 146 | * @group excel |
| 147 | */ |
| 148 | public function testSetFormula() |
| 149 | { |
| 150 | $arr = [['=A4*B4']]; |
| 151 | |
| 152 | $workbookRange = $this->_client->createRequest( |
| 153 | "PATCH", |
| 154 | "/me/drive/items/" . |
| 155 | $this->_fileId . |
| 156 | "/workbook/worksheets/SetFormula/range(address='C4')" |
| 157 | ) |
| 158 | ->attachBody(array("formulas" => $arr)) |
| 159 | ->setReturnType(Model\WorkbookRange::class) |
| 160 | ->execute(); |
| 161 | $this->assertNotNull($workbookRange); |
| 162 | $this->assertEquals($arr, $workbookRange->getFormulas()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @group functional |
| 167 | * @group excel |
| 168 | */ |
| 169 | public function testAddTableUsedRange() |
| 170 | { |
| 171 | $workbookRange = $this->_client->createRequest( |
| 172 | "GET", |
| 173 | "/me/drive/items/" . |
| 174 | $this->_fileId . |
| 175 | "/workbook/worksheets/AddTableUsedRange/usedrange" |
| 176 | ) |
| 177 | ->setReturnType(Model\WorkbookRange::class) |
| 178 | ->execute(); |
| 179 | |
| 180 | $data = array("address" => $workbookRange->getAddress(), "hasHeaders" => false); |
| 181 | |
| 182 | $workbookTable = $this->_client->createRequest("POST", "/me/drive/items/" . $this->_fileId . "/workbook/worksheets/AddTableUsedRange/tables") |
| 183 | ->attachBody($data) |
| 184 | ->setReturnType(Model\WorkbookTable::class); |
| 185 | |
| 186 | $this->assertNotNull($workbookTable); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @group functional |
| 191 | * @group excel |
| 192 | */ |
| 193 | public function testAddRowToTable() |
| 194 | { |
| 195 | $newWorkbookTableRow = new Model\WorkbookTableRow(); |
| 196 | $newWorkbookTableRow->setIndex(0); |
| 197 | $arr = [["ValueA2", "ValueA3"]]; |
| 198 | $newWorkbookTableRow->setValues($arr); |
| 199 | |
| 200 | $workbookTableRow = $this->_client->createRequest( |
| 201 | "POST", |
| 202 | "/me/drive/items/" . |
| 203 | $this->_fileId . |
| 204 | "/workbook/tables/Table1/Rows" |
| 205 | ) |
| 206 | ->attachBody($newWorkbookTableRow) |
| 207 | ->setReturnType(Model\WorkbookRange::class) |
| 208 | ->execute(); |
| 209 | |
| 210 | $this->assertNotNull($workbookTableRow); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @group functional |
| 215 | * @group excel |
| 216 | */ |
| 217 | public function testSortTable() |
| 218 | { |
| 219 | $sortField = new Model\WorkbookSortField(); |
| 220 | $sortField->setAscending(true); |
| 221 | $sortField->setSortOn("Value"); |
| 222 | $sortField->setKey(0); |
| 223 | |
| 224 | $workbookSortFields = $this->_client->createRequest( |
| 225 | "POST", |
| 226 | "/me/drive/items/" . |
| 227 | $this->_fileId . |
| 228 | "/workbook/tables/Table2/sort/apply" |
| 229 | ) |
| 230 | ->attachBody('{"fields":'. json_encode(array($sortField)) . '}') |
| 231 | ->setReturnType(Model\WorkbookSortField::class) |
| 232 | ->execute(); |
| 233 | |
| 234 | $this->assertNotNull($workbookSortFields); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @group functional |
| 239 | * @group excel |
| 240 | */ |
| 241 | public function testFilterTableValues() |
| 242 | { |
| 243 | $this->_client->createRequest( |
| 244 | "POST", |
| 245 | "/me/drive/items/" . |
| 246 | $this->_fileId . |
| 247 | "/workbook/tables/FilterTableValues/columns/1/filter/applyvaluesfilter" |
| 248 | ) |
| 249 | ->attachBody('{"values":["2"]}') |
| 250 | ->setReturnType(Model\WorkbookSortField::class) |
| 251 | ->execute(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @group functional |
| 256 | * @group excel |
| 257 | */ |
| 258 | public function testCreateChartFromTable() |
| 259 | { |
| 260 | $tableRange = $this->_client->createRequest( |
| 261 | "GET", |
| 262 | "/me/drive/items/" . |
| 263 | $this->_fileId . |
| 264 | "/workbook/tables/CreateChartFromTable/range" |
| 265 | ) |
| 266 | ->setReturnType(Model\WorkbookRange::class) |
| 267 | ->execute(); |
| 268 | $address = $tableRange->getAddress(); |
| 269 | |
| 270 | $workbookChart = $this->_client->createRequest( |
| 271 | "POST", |
| 272 | "/me/drive/items/" . |
| 273 | $this->_fileId . |
| 274 | "/workbook/worksheets/CreateChartFromTable/charts/add" |
| 275 | ) |
| 276 | ->attachBody(array("type" => "ColumnStacked", "sourceData" => "$address", "seriesBy" => "Auto")) |
| 277 | ->setReturnType(Model\WorkbookChart::class) |
| 278 | ->execute(); |
| 279 | $this->assertNotNull($workbookChart); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @group functional |
| 284 | * @group excel |
| 285 | */ |
| 286 | public function testProtectWorksheet() |
| 287 | { |
| 288 | try{ |
| 289 | $this->_client->createRequest( |
| 290 | "POST", |
| 291 | "/me/drive/items/" . |
| 292 | $this->_fileId . |
| 293 | "/workbook/worksheets/ProtectWorksheet/protection/protect" |
| 294 | ) |
| 295 | ->execute(); |
| 296 | |
| 297 | $dummyWorkbookRange = new Model\WorkbookRange(); |
| 298 | $dummyWorkbookRange->setValues('[["This should not work"]]'); |
| 299 | |
| 300 | $workbookRange = $this->_client->createRequest( |
| 301 | "PATCH", |
| 302 | "/me/drive/items/" . |
| 303 | $this->_fileId . |
| 304 | "/workbook/worksheets('protectworksheet')/cell(row=1,column=1)" |
| 305 | ) |
| 306 | ->attachBody($dummyWorkbookRange) |
| 307 | ->execute(); |
| 308 | } catch(Exception $e) |
| 309 | { |
| 310 | //403: Forbidden - file is locked for editing |
| 311 | $this->assertEquals(403, $e->getResponse()->getStatusCode()); |
| 312 | |
| 313 | $this->_client->createRequest( |
| 314 | "POST", |
| 315 | "/me/drive/items/" . |
| 316 | $this->_fileId . |
| 317 | "/workbook/worksheets/ProtectWorksheet/protection/unprotect" |
| 318 | ) |
| 319 | ->execute(); |
| 320 | } |
| 321 | } |
| 322 | } |