Skip to content

Commit

Permalink
add new get term authors function with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dwainm committed May 21, 2015
1 parent b3ead3d commit 18676be
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
62 changes: 62 additions & 0 deletions classes/class-sensei-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1695,4 +1695,66 @@ public function remove_courses_menu_model_taxonomy(){

}// end remove courses module tax

/**
* Determine the author of a modules taxonomy term by looking at
* the prefixed author id. Will return the admin user author could not be determined.
*
* @since 1.8.0
*
* @param string $term_name
* @return array $owners { typ WP_User }. Empty array if none if found.
*/
public static function get_term_authors( $term_name ){

$terms = get_terms( array( 'module') , array( 'name__like'=>$term_name, 'hide_empty' => false ) );

$owners = array();
if( empty( $terms ) ){

return $owners;

}

// setup the admin user
$admin_user = get_user_by( 'email', get_bloginfo( 'admin_email' ) );

//if there are more handle them appropriately and get the ones we really need that matches the desired name exactly
foreach( $terms as $term){
if( $term->name == $term_name ){

// look for the author in the slug
$slug_parts = explode( '-', $term->slug );

if( ! ( count( $slug_parts ) > 1 ) ){

$owners[] = $admin_user;

}else{

// get the user data
$possible_user_id = $slug_parts[0];
$author = get_userdata( $possible_user_id );

// if the user doesnt exist for the first part of the slug
// then this slug was also created by admin
if( ! $author ){

$owners[] = $admin_user;

}else{

$owners[] = $author;

} // end if author

}// end if slug parts

}// end if term name

} // end for each

return $owners;

}// end get_term_author

} // end modules class
80 changes: 80 additions & 0 deletions tests/test-class-modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

class Sensei_Class_Modules_Test extends WP_UnitTestCase {

/**
* Constructor function
*/
public function __construct(){
parent::__construct();
include_once( 'factory/Sensei-Factory.php' );
}


/**
* setup function
* This function sets up the lessons, quizes and their questions. This function runs before
* every single test in this class
*/
public function setup(){

}// end function setup()

/**
* Testing the quiz class to make sure it is loaded
*/
public function testClassInstance() {

//test if the global sensei quiz class is loaded
$this->assertTrue( isset( Sensei()->modules ), 'Sensei Modules class is not loaded' );

} // end testClassInstance

/**
* Testing Sensei_Core_Modules::get_term_author
*/
public function testGetTermAuthor(){

// setup assertions
$test_user_id = wp_create_user( 'teacherGetTermAuthor', 'teacherGetTermAuthor', 'teacherGetTermAuthor@test.com' );

//insert a general term
wp_insert_term('Get Started', 'module');
//insert a term as if from the user
wp_insert_term('Get Started Today', 'module', array(
'description'=> 'A yummy apple.',
'slug' => $test_user_id . '-get-started-today'
));

// does the function exist?
$this->assertTrue( method_exists( 'Sensei_Core_Modules', 'get_term_authors'), 'The function Sensei_Core_Modules::get_term_author does not exist ');

// does the taxonomy exist
$module_taxonomy = get_taxonomy('module');
$this->assertTrue( $module_taxonomy->public , 'The module taxonomy is not loaded' );

// does it return empty array id for bogus term nam?
$term_authors = Sensei_Core_Modules::get_term_authors( 'bogusnonexistan' );
$this->assertTrue( empty( $term_authors ) , 'The function should return false for an invalid term' );

//does it return the admin user for a valid term ?
$admin = get_user_by( 'email', get_bloginfo('admin_email') );
$term_authors = Sensei_Core_Modules::get_term_authors( 'Get Started' );
$this->assertTrue( $admin == $term_authors[0] , 'The function should return admin user for normal module term.' );

// does it return the expected new user for the given term registered with that id in front of the slug?
$term_authors = Sensei_Core_Modules::get_term_authors( 'Get Started Today' );
$this->assertTrue( get_userdata( $test_user_id ) == $term_authors[0], 'The function should admin user for normal module term.' );

// what about terms with the same name but different slug?
// It should return 2 authors as we've created 2 with the same name
// insert a term that is the same as the first one
wp_insert_term('Get Started', 'module', array(
'description'=> 'A yummy apple.',
'slug' => $test_user_id . '-get-started'
));
$term_authors = Sensei_Core_Modules::get_term_authors( 'Get Started' );
$this->assertTrue( 2 == count( $term_authors ), 'The function should admin user for normal module term.' );
}

} // end class

0 comments on commit 18676be

Please sign in to comment.