loginAsAdmin(); } public function test_permission_callback_requires_admin_capability() { wp_set_current_user(0); $this->assertFalse(WCProductController::permissionCallback()); $sub = self::factory()->user->create(['role' => 'subscriber']); wp_set_current_user($sub); $this->assertFalse(WCProductController::permissionCallback()); $editor = self::factory()->user->create(['role' => 'editor']); wp_set_current_user($editor); $this->assertFalse(WCProductController::permissionCallback()); $admin = self::factory()->user->create(['role' => 'administrator']); wp_set_current_user($admin); $this->assertTrue(WCProductController::permissionCallback()); } public function test_get_without_product_id_returns_400() { $res = WCProductController::handle($this->getRequest([])); $this->assertSame(400, $res->get_status()); $this->assertSame('product_id required', $res->get_data()['error']); } public function test_get_with_non_product_post_returns_400() { $postId = self::factory()->post->create(); $res = WCProductController::handle($this->getRequest(['product_id' => $postId])); $this->assertSame(400, $res->get_status()); $this->assertSame('not a product', $res->get_data()['error']); } public function test_get_returns_name_short_description_prices_and_image_meta() { $pid = $this->createProduct([ 'post_title' => 'Hat', 'post_excerpt' => 'A summary', 'post_content' => 'The long description.', ]); $product = wc_get_product($pid); $product->set_regular_price('20'); $product->set_sale_price('15'); $product->save(); $res = WCProductController::handle($this->getRequest(['product_id' => $pid])); $this->assertSame(200, $res->get_status()); $data = $res->get_data(); $this->assertSame('Hat', $data['name']); $this->assertSame('A summary', $data['short_description']); $this->assertSame('The long description.', $data['description']); $this->assertSame('20', $data['regular_price']); $this->assertSame('15', $data['sale_price']); $this->assertSame(0, $data['image_id']); $this->assertSame('', $data['image_url']); } public function test_post_updates_name() { $pid = $this->createProduct(['post_title' => 'Old']); $res = WCProductController::handle($this->postRequest([ 'product_id' => $pid, 'field' => 'name', 'value' => 'New title', ])); $this->assertSame(200, $res->get_status()); $this->assertTrue($res->get_data()['ok']); $this->assertSame('New title', get_post($pid)->post_title); } public function test_post_name_sanitizes_html() { $pid = $this->createProduct(); WCProductController::handle($this->postRequest([ 'product_id' => $pid, 'field' => 'name', 'value' => 'Clean', ])); $this->assertSame('Clean', get_post($pid)->post_title); } public function test_post_updates_short_description() { $pid = $this->createProduct(['post_excerpt' => 'old']); $res = WCProductController::handle($this->postRequest([ 'product_id' => $pid, 'field' => 'short_description', 'value' => '
A rich excerpt
', ])); $this->assertSame(200, $res->get_status()); $this->assertStringContainsString('rich', get_post($pid)->post_excerpt); } public function test_post_updates_description() { $pid = $this->createProduct(['post_content' => 'old long description']); $res = WCProductController::handle($this->postRequest([ 'product_id' => $pid, 'field' => 'description', 'value' => 'A rich long description
', ])); $this->assertSame(200, $res->get_status()); $this->assertStringContainsString('rich', get_post($pid)->post_content); } public function test_post_description_sanitizes_disallowed_html() { $pid = $this->createProduct(); WCProductController::handle($this->postRequest([ 'product_id' => $pid, 'field' => 'description', 'value' => 'safe
', ])); $this->assertStringNotContainsString('