| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration\QuickEdit\Controllers; |
| 4 |
|
| 5 |
use Extendify\QuickEdit\Controllers\SiteIdentityController; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
class SiteIdentityControllerTest extends WP_UnitTestCase |
| 9 |
{ |
| 10 |
public function test_permission_callback_requires_manage_options() |
| 11 |
{ |
| 12 |
wp_set_current_user(0); |
| 13 |
$this->assertFalse(SiteIdentityController::permissionCallback()); |
| 14 |
|
| 15 |
$editor = self::factory()->user->create(['role' => 'editor']); |
| 16 |
wp_set_current_user($editor); |
| 17 |
$this->assertFalse(SiteIdentityController::permissionCallback()); |
| 18 |
|
| 19 |
$admin = self::factory()->user->create(['role' => 'administrator']); |
| 20 |
wp_set_current_user($admin); |
| 21 |
$this->assertTrue(SiteIdentityController::permissionCallback()); |
| 22 |
} |
| 23 |
|
| 24 |
public function test_get_returns_title_tagline_logo_id_and_url() |
| 25 |
{ |
| 26 |
update_option('blogname', 'My Site'); |
| 27 |
update_option('blogdescription', 'Tagline & co'); |
| 28 |
delete_option('site_logo'); |
| 29 |
|
| 30 |
$res = SiteIdentityController::handle($this->getRequest()); |
| 31 |
|
| 32 |
$this->assertSame(200, $res->get_status()); |
| 33 |
$data = $res->get_data(); |
| 34 |
$this->assertSame('My Site', $data['title']); |
| 35 |
// WP escapes & to & on store; controller decodes on read. |
| 36 |
$this->assertSame('Tagline & co', $data['tagline']); |
| 37 |
$this->assertSame(0, $data['logo_id']); |
| 38 |
$this->assertSame('', $data['logo_url']); |
| 39 |
} |
| 40 |
|
| 41 |
public function test_get_returns_decoded_entities_so_react_input_doesnt_show_them() |
| 42 |
{ |
| 43 |
// wp_specialchars_decode round-trip: WP stores ' for apostrophe. |
| 44 |
update_option('blogname', "Brand's site"); |
| 45 |
|
| 46 |
$res = SiteIdentityController::handle($this->getRequest()); |
| 47 |
|
| 48 |
$this->assertSame("Brand's site", $res->get_data()['title']); |
| 49 |
} |
| 50 |
|
| 51 |
public function test_get_returns_logo_id_when_site_logo_set_even_without_attachment_url() |
| 52 |
{ |
| 53 |
// No real attachment file — just an ID. Controller still returns the id |
| 54 |
// and an empty url string (wp_get_attachment_image_url returns false). |
| 55 |
update_option('site_logo', 12345); |
| 56 |
|
| 57 |
$res = SiteIdentityController::handle($this->getRequest()); |
| 58 |
|
| 59 |
$data = $res->get_data(); |
| 60 |
$this->assertSame(12345, $data['logo_id']); |
| 61 |
$this->assertSame('', $data['logo_url']); |
| 62 |
} |
| 63 |
|
| 64 |
public function test_post_updates_blogname() |
| 65 |
{ |
| 66 |
$res = SiteIdentityController::handle($this->postRequest(['title' => 'New Name'])); |
| 67 |
|
| 68 |
$this->assertSame(200, $res->get_status()); |
| 69 |
$this->assertTrue($res->get_data()['ok']); |
| 70 |
$this->assertSame('New Name', get_option('blogname')); |
| 71 |
} |
| 72 |
|
| 73 |
public function test_post_updates_tagline() |
| 74 |
{ |
| 75 |
$res = SiteIdentityController::handle($this->postRequest(['tagline' => 'New tagline'])); |
| 76 |
|
| 77 |
$this->assertSame(200, $res->get_status()); |
| 78 |
$this->assertSame('New tagline', get_option('blogdescription')); |
| 79 |
} |
| 80 |
|
| 81 |
public function test_post_sanitizes_title_strips_html() |
| 82 |
{ |
| 83 |
$res = SiteIdentityController::handle($this->postRequest([ |
| 84 |
'title' => '<script>alert(1)</script>Clean', |
| 85 |
])); |
| 86 |
|
| 87 |
$this->assertSame(200, $res->get_status()); |
| 88 |
// sanitize_text_field strips tags. |
| 89 |
$this->assertSame('Clean', get_option('blogname')); |
| 90 |
} |
| 91 |
|
| 92 |
public function test_post_with_logo_id_zero_deletes_site_logo() |
| 93 |
{ |
| 94 |
update_option('site_logo', 999); |
| 95 |
|
| 96 |
$res = SiteIdentityController::handle($this->postRequest(['logo_id' => 0])); |
| 97 |
|
| 98 |
$this->assertSame(200, $res->get_status()); |
| 99 |
$this->assertFalse(get_option('site_logo')); |
| 100 |
} |
| 101 |
|
| 102 |
public function test_post_with_logo_id_nonzero_sets_site_logo() |
| 103 |
{ |
| 104 |
delete_option('site_logo'); |
| 105 |
|
| 106 |
$res = SiteIdentityController::handle($this->postRequest(['logo_id' => 42])); |
| 107 |
|
| 108 |
$this->assertSame(200, $res->get_status()); |
| 109 |
$this->assertSame('42', (string) get_option('site_logo')); |
| 110 |
} |
| 111 |
|
| 112 |
public function test_post_with_missing_logo_id_key_leaves_existing_logo_intact() |
| 113 |
{ |
| 114 |
update_option('site_logo', 77); |
| 115 |
|
| 116 |
$res = SiteIdentityController::handle($this->postRequest(['title' => 'X'])); |
| 117 |
|
| 118 |
$this->assertSame(200, $res->get_status()); |
| 119 |
$this->assertSame('77', (string) get_option('site_logo')); |
| 120 |
} |
| 121 |
|
| 122 |
public function test_post_with_no_known_keys_returns_ok_without_changing_options() |
| 123 |
{ |
| 124 |
update_option('blogname', 'Untouched'); |
| 125 |
update_option('blogdescription', 'Same'); |
| 126 |
|
| 127 |
$res = SiteIdentityController::handle($this->postRequest(['junk' => 'value'])); |
| 128 |
|
| 129 |
$this->assertSame(200, $res->get_status()); |
| 130 |
$this->assertSame('Untouched', get_option('blogname')); |
| 131 |
$this->assertSame('Same', get_option('blogdescription')); |
| 132 |
} |
| 133 |
|
| 134 |
public function test_round_trip_get_post_get() |
| 135 |
{ |
| 136 |
SiteIdentityController::handle($this->postRequest([ |
| 137 |
'title' => 'After', |
| 138 |
'tagline' => 'Tag', |
| 139 |
])); |
| 140 |
|
| 141 |
$res = SiteIdentityController::handle($this->getRequest()); |
| 142 |
|
| 143 |
$data = $res->get_data(); |
| 144 |
$this->assertSame('After', $data['title']); |
| 145 |
$this->assertSame('Tag', $data['tagline']); |
| 146 |
} |
| 147 |
|
| 148 |
public function test_init_hooks_registerRoutes_into_rest_api_init() |
| 149 |
{ |
| 150 |
remove_all_filters('rest_api_init'); |
| 151 |
SiteIdentityController::init(); |
| 152 |
|
| 153 |
$this->assertNotFalse( |
| 154 |
has_action('rest_api_init', [SiteIdentityController::class, 'registerRoutes']) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
private function getRequest(): \WP_REST_Request |
| 159 |
{ |
| 160 |
$req = new \WP_REST_Request('GET', '/extendify/v1/quick-edit/site-identity'); |
| 161 |
return $req; |
| 162 |
} |
| 163 |
|
| 164 |
private function postRequest(array $body): \WP_REST_Request |
| 165 |
{ |
| 166 |
$req = new \WP_REST_Request('POST', '/extendify/v1/quick-edit/site-identity'); |
| 167 |
$req->set_header('Content-Type', 'application/json'); |
| 168 |
$req->set_body(wp_json_encode($body)); |
| 169 |
return $req; |
| 170 |
} |
| 171 |
} |
| 172 |
|