|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * BEdita, API-first content management framework |
| 6 | + * Copyright 2025 ChannelWeb Srl, Chialab Srl |
| 7 | + * |
| 8 | + * This file is part of BEdita: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
| 14 | + */ |
| 15 | + |
| 16 | +namespace BEdita\API\Test\TestCase\Controller; |
| 17 | + |
| 18 | +use BEdita\API\TestSuite\IntegrationTestCase; |
| 19 | +use Cake\ORM\TableRegistry; |
| 20 | + |
| 21 | +/** |
| 22 | + * @coversDefaultClass \BEdita\API\Controller\TreePathsController |
| 23 | + */ |
| 24 | +class TreePathsControllerTest extends IntegrationTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Test `index` with a valid slug. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + * @covers ::index() |
| 31 | + * @covers ::pathDetails() |
| 32 | + */ |
| 33 | + public function testIndexValidSlugPath(): void |
| 34 | + { |
| 35 | + $this->configRequestHeaders(); |
| 36 | + $this->get('/tree_paths/root-folder-11/sub-folder-12'); |
| 37 | + |
| 38 | + $this->assertResponseCode(200); |
| 39 | + $this->assertContentType('application/vnd.api+json'); |
| 40 | + |
| 41 | + $response = json_decode((string)$this->_response->getBody(), true); |
| 42 | + |
| 43 | + $expected = [ |
| 44 | + 'data' => [ |
| 45 | + 'id' => '12', |
| 46 | + 'type' => 'folders', |
| 47 | + 'attributes' => [ |
| 48 | + 'status' => 'on', |
| 49 | + 'uname' => 'sub-folder', |
| 50 | + 'title' => 'Sub Folder', |
| 51 | + 'description' => 'sub folder of root folder', |
| 52 | + 'body' => null, |
| 53 | + 'extra' => null, |
| 54 | + 'lang' => 'en', |
| 55 | + 'publish_start' => null, |
| 56 | + 'publish_end' => null, |
| 57 | + 'children_order' => null, |
| 58 | + 'menu' => true, |
| 59 | + ], |
| 60 | + 'meta' => [ |
| 61 | + 'locked' => false, |
| 62 | + 'path' => '/11/12', |
| 63 | + 'slug_path' => [ |
| 64 | + [ |
| 65 | + 'id' => 11, |
| 66 | + 'menu' => false, |
| 67 | + 'params' => null, |
| 68 | + 'slug' => 'root-folder-11', |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'id' => 12, |
| 72 | + 'menu' => true, |
| 73 | + 'params' => null, |
| 74 | + 'slug' => 'sub-folder-12', |
| 75 | + ], |
| 76 | + ], |
| 77 | + 'published' => null, |
| 78 | + 'created_by' => 1, |
| 79 | + 'modified_by' => 1, |
| 80 | + 'created' => '2018-01-31T07:09:23+00:00', |
| 81 | + 'modified' => '2018-01-31T08:30:00+00:00', |
| 82 | + ], |
| 83 | + 'relationships' => [ |
| 84 | + 'translations' => [ |
| 85 | + 'links' => [ |
| 86 | + 'related' => 'http://api.example.com/folders/12/translations', |
| 87 | + 'self' => 'http://api.example.com/folders/12/relationships/translations', |
| 88 | + ], |
| 89 | + ], |
| 90 | + 'children' => [ |
| 91 | + 'links' => [ |
| 92 | + 'related' => 'http://api.example.com/folders/12/children', |
| 93 | + 'self' => 'http://api.example.com/folders/12/relationships/children', |
| 94 | + ], |
| 95 | + ], |
| 96 | + 'parent' => [ |
| 97 | + 'links' => [ |
| 98 | + 'related' => 'http://api.example.com/folders/12/parent', |
| 99 | + 'self' => 'http://api.example.com/folders/12/relationships/parent', |
| 100 | + ], |
| 101 | + ], |
| 102 | + ], |
| 103 | + ], |
| 104 | + 'links' => [ |
| 105 | + 'self' => 'http://api.example.com/tree_paths', |
| 106 | + 'home' => 'http://api.example.com/home', |
| 107 | + ], |
| 108 | + 'meta' => [ |
| 109 | + 'schema' => [ |
| 110 | + 'folders' => [ |
| 111 | + '$id' => 'http://api.example.com/model/schema/folders', |
| 112 | + 'revision' => '3048758948', |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], |
| 116 | + ]; |
| 117 | + |
| 118 | + static::assertEquals($expected, $response); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test `index` with an invalid slug. |
| 123 | + * |
| 124 | + * @return void |
| 125 | + * @covers ::index() |
| 126 | + */ |
| 127 | + public function testIndexInvalidSlugPath(): void |
| 128 | + { |
| 129 | + $this->configRequestHeaders(); |
| 130 | + $this->get('/tree_paths/pippo/pluto'); |
| 131 | + |
| 132 | + $this->assertResponseCode(404); |
| 133 | + $this->assertContentType('application/vnd.api+json'); |
| 134 | + |
| 135 | + $response = json_decode((string)$this->_response->getBody(), true); |
| 136 | + |
| 137 | + static::assertEquals($response['error']['title'], 'Invalid path'); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Verify that two object with the same slug are reachable with different paths. |
| 142 | + * |
| 143 | + * @return void |
| 144 | + * @covers ::index() |
| 145 | + */ |
| 146 | + public function testObjectsWithSameSlugDifferentPaths(): void |
| 147 | + { |
| 148 | + $treesTable = TableRegistry::getTableLocator()->get('Trees'); |
| 149 | + |
| 150 | + // Create a new object with the same slug but different paths |
| 151 | + $newTreeNode = $treesTable->newEntity([ |
| 152 | + 'object_id' => 14, |
| 153 | + 'parent_id' => 13, // Another-root-folder-13 |
| 154 | + 'root_id' => 13, |
| 155 | + 'parent_node_id' => 5, |
| 156 | + 'tree_left' => 10, |
| 157 | + 'tree_right' => 11, |
| 158 | + 'depth_level' => 1, |
| 159 | + 'menu' => 1, |
| 160 | + 'canonical' => 1, |
| 161 | + 'slug' => 'gustavo-supporto-profile-4', |
| 162 | + ]); |
| 163 | + |
| 164 | + $treesTable->saveOrFail($newTreeNode); |
| 165 | + |
| 166 | + $this->configRequestHeaders(); |
| 167 | + // First object in "another-root-folder-13/gustavo-supporto-profile-4" |
| 168 | + $this->get('/tree_paths/another-root-folder-13/gustavo-supporto-profile-4'); |
| 169 | + $this->assertResponseCode(200); |
| 170 | + $this->assertContentType('application/vnd.api+json'); |
| 171 | + $response1 = json_decode((string)$this->_response->getBody(), true); |
| 172 | + |
| 173 | + $this->configRequestHeaders(); |
| 174 | + // Second object in "root-folder-11/sub-folder-12/gustavo-supporto-profile-4" |
| 175 | + $this->get('/tree_paths/root-folder-11/sub-folder-12/gustavo-supporto-profile-4'); |
| 176 | + $this->assertResponseCode(200); |
| 177 | + $this->assertContentType('application/vnd.api+json'); |
| 178 | + $response2 = json_decode((string)$this->_response->getBody(), true); |
| 179 | + |
| 180 | + static::assertNotEquals( |
| 181 | + $response1['data']['id'], |
| 182 | + $response2['data']['id'], |
| 183 | + 'Object IDS must be different even if they have the same slug' |
| 184 | + ); |
| 185 | + |
| 186 | + static::assertNotEquals( |
| 187 | + $response1['data']['meta']['extra']['slug_path'], |
| 188 | + $response2['data']['meta']['extra']['slug_path'], |
| 189 | + 'Paths must me different for the same slug.' |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments