Index: tests/phpunit/data/images/a2-small-100x75.jpg
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/wp-includes/taxonomy.php
===================================================================
--- src/wp-includes/taxonomy.php	(revision 25499)
+++ src/wp-includes/taxonomy.php	(working copy)
@@ -2091,9 +2091,24 @@
 	$name = wp_unslash($name);
 	$description = wp_unslash($description);
 
-	if ( empty($slug) )
-		$slug = sanitize_title($name);
+	if ( empty( $slug ) ) {
+		$name = trim( $name );
+		$sanitized_name = sanitize_title( $name );
+		$exists = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, slug FROM $wpdb->terms WHERE name = %s ORDER BY term_id ASC LIMIT 1", $name ) );
+		if ( ! empty( $exists ) && term_exists( $exists->term_id, $taxonomy ) ) {
+			if ( $sanitized_name === $exists->slug )
+				return new WP_Error( 'term_exists', __( 'A term with the name provided already exists.' ) );
 
+			$slug = $exists->slug;
+		} elseif ( ! empty( $exists ) ) {
+			$exists = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s ORDER BY term_id ASC LIMIT 1", $sanitized_name ) );
+			if ( ! empty( $exists ) )
+				return new WP_Error( 'term_exists', __( 'A term with the name provided already exists.' ) );
+		} else {
+			$slug = $sanitized_name;
+		}
+	}
+
 	$term_group = 0;
 	if ( $alias_of ) {
 		$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
