justReturn(5); Functions\when('current_user_can')->justReturn(false); Functions\when('absint')->alias(static fn ($v): int => abs((int) $v)); Functions\when('sanitize_text_field')->returnArg(); $this->questions = Mockery::mock(QuestionRepository::class); $this->offerings = Mockery::mock(OfferingRepository::class); $this->endpoint = new QuestionEndpoint($this->questions, $this->offerings); // The caller (instructor 5) owns offering 9, so the ownership gate passes // and validation is reached. $this->offerings->shouldReceive('findById')->with(9)->andReturn( new Offering(instructorId: 5, kind: Offering::KIND_GROUP_CLASS, title: 'Choir', id: 9) ); } public function testCreateRejectsLabelLongerThanColumnLimit(): void { // The insert must never be attempted for an over-long label — the bug was // that it reached the DB, silently failed, and returned success anyway. $this->questions->shouldNotReceive('insert'); $request = new \WP_REST_Request([ 'offering_id' => 9, 'label' => str_repeat('a', Question::MAX_LABEL_LENGTH + 1), ]); $response = $this->endpoint->create($request); self::assertInstanceOf(\WP_Error::class, $response); self::assertSame(400, $response->error_data['invalid_question']['status']); } public function testCreateAcceptsLabelAtColumnLimit(): void { $this->questions->shouldReceive('insert')->once()->andReturn(42); $request = new \WP_REST_Request([ 'offering_id' => 9, 'label' => str_repeat('a', Question::MAX_LABEL_LENGTH), ]); $response = $this->endpoint->create($request); self::assertInstanceOf(\WP_REST_Response::class, $response); self::assertSame(201, $response->get_status()); } }