| 36 | |
| 37 | /** |
| 38 | * @ticket 21003 |
| 39 | */ |
| 40 | function test_orderby_meta() { |
| 41 | $comment_id = $this->factory->comment->create(); |
| 42 | $comment_id2 = $this->factory->comment->create(); |
| 43 | $comment_id3 = $this->factory->comment->create(); |
| 44 | |
| 45 | add_comment_meta( $comment_id, 'key', 'value1', true ); |
| 46 | add_comment_meta( $comment_id, 'key1', 'value1', true ); |
| 47 | add_comment_meta( $comment_id, 'key3', 'value3', true ); |
| 48 | add_comment_meta( $comment_id2, 'key', 'value2', true ); |
| 49 | add_comment_meta( $comment_id2, 'key2', 'value2', true ); |
| 50 | add_comment_meta( $comment_id3, 'key3', 'value3', true ); |
| 51 | |
| 52 | $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ) ) ); |
| 53 | $this->assertEquals( 2, count( $comments ) ); |
| 54 | $this->assertEquals( $comment_id2, $comments[0]->comment_ID ); |
| 55 | $this->assertEquals( $comment_id, $comments[1]->comment_ID ); |
| 56 | |
| 57 | $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ), 'order' => 'ASC' ) ); |
| 58 | $this->assertEquals( 2, count( $comments ) ); |
| 59 | $this->assertEquals( $comment_id, $comments[0]->comment_ID ); |
| 60 | $this->assertEquals( $comment_id2, $comments[1]->comment_ID ); |
| 61 | |
| 62 | $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) ); |
| 63 | $this->assertEquals( 2, count( $comments ) ); |
| 64 | $this->assertEquals( $comment_id, $comments[0]->comment_ID ); |
| 65 | $this->assertEquals( $comment_id3, $comments[1]->comment_ID ); |
| 66 | |
| 67 | // value1 is present on two different keys for $comment_id yet we should get only one instance |
| 68 | // of that comment in the results |
| 69 | $comments = get_comments( array( 'meta_value' => 'value1', 'orderby' => array( 'meta_value' ) ) ); |
| 70 | $this->assertEquals( 1, count( $comments ) ); |
| 71 | } |